Skip to content

Instantly share code, notes, and snippets.

@nomaddo
Last active April 19, 2018 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nomaddo/a2105de5752d6f2481d1c8a73a7fca12 to your computer and use it in GitHub Desktop.
Save nomaddo/a2105de5752d6f2481d1c8a73a7fca12 to your computer and use it in GitHub Desktop.
これはどうすればいいの、、、、
#include <memory>
class A : public std::enable_shared_from_this<A> {
public:
int id;
A(int x) : id(x) {}
};
class B : public A
{
public:
B() : id(1000);
};
int main() {
A a(100);
a.shared_from_this();
B b;
std::shared_ptr<B> bad = b.shared_from_this();
}
//
// /home/nomaddo/CLionProjects/test/main.cpp: In function ‘int main()’:
// /home/nomaddo/CLionProjects/test/main.cpp:20:29: error: conversion from ‘std::shared_ptr<A>’ to non-scalar type ‘B’ requested
// B bad = b.shared_from_this();
// ^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment