0

Is it possible to create a smart pointer to an object and pass this smart pointer as an argument to the constructor? I would like to use a smart pointer to the object in the object itself.

auto player1 = std::make_shared<Player>(argument1, ..smartpointer_to_player1..);
George P.
  • 181
  • 1
  • 8

2 Answers2

3

If you want a class to have a shared_ptr to itself (or, rather, its this pointer), there is an alternative way to accomplish this instead of your proposed solution via std::enable_shared_from_this. For example:

class Player : public std::enable_shared_from_this<Player> {

//... definition

   std::shared_ptr<Player> GetPtr() { return shared_from_this(); }
};

By inheriting this class, it allows you to get a shared_ptr to the this pointer by calling the inherited member function shared_from_this in any of the class' member functions (although this function would not be available in the constructor). One limitation of this is that the object would need to have been stored in a shared_ptr previously or this will caused undefined behavior (before C++17) or throw std::bad_weak_ptr (C++17 and after).

In any case, this will likely give you the functionality you would like and avoid having to pass a shared_ptr to itself via the constructor.

Daniel
  • 1,291
  • 6
  • 15
  • 2
    Beware that `shared_from_this()` isn't available in the constructor. This seems like it solves OP's problem, but the question does mention the constructor a few times. – François Andrieux May 09 '18 at 18:41
  • 1
    Using `shared_from_this` on an instance that isn't managed by a `shared_ptr` is only undefined behavior until c++17. In c++17 and later it throws [`std::bad_weak_ptr`](http://en.cppreference.com/w/cpp/memory/bad_weak_ptr). – François Andrieux May 09 '18 at 18:43
  • Unfortunately, I can not get it to work. If I create a shared pointer `std::shared_ptr player2 = std::make_shared();` the polymorphic behavior is correct, but if I try to get a pointer I get an error `std::bad_weak_ptr`. If I create it this way: `std::shared_ptr player2 = std::make_shared();`, then there is no error, but there is no polymorphic behavior either (the overridden functions for Subplayer are not being used, Subplayer is a subclass) – George P. May 09 '18 at 19:30
  • @FrançoisAndrieux you're right about the constructor and the thrown exception c++17 and after. I was still thinking in c++14 rules :) – Daniel May 09 '18 at 19:59
  • @GeorgeP. I'm not sure I follow, and I would need to see some code to see what you are doing (possibly in a new question) – Daniel May 09 '18 at 20:00
  • @Daniel, the code and the initial problem is here: https://stackoverflow.com/questions/50257337/how-to-run-a-function-of-an-actual-object-which-is-stored-in-the-container-usin – George P. May 09 '18 at 20:05
1

If the object needs a shared pointer to itself it should inherit from std::enable_shared_from_this and then obtain the shared pointer by calling shared_from_this() in the member function where it needs it.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • You cannot use `shared_from_this` in the object's constructor. See [this question](https://stackoverflow.com/questions/31924396/why-shared-from-this-cant-be-used-in-constructor-from-technical-standpoint). – François Andrieux May 09 '18 at 18:44
  • Unfortunately, I can not get it to work. If I create a shared pointer `std::shared_ptr player2 = std::make_shared();` the polymorphic behavior is correct, but if I try to get a pointer I get an error `std::bad_weak_ptr`. If I create it this way: `std::shared_ptr player2 = std::make_shared();`, then there is no error, but there is no polymorphic behavior either (the overridden functions for Subplayer(subclass) are not being used). https://stackoverflow.com/questions/50257337/how-to-run-a-function-of-an-actual-object-which-is-stored-in-the-container-usin – George P. May 09 '18 at 19:43