-2

Say I have a class similar to this.

class Actor {
public:
    add()
    {
        auto tmp = std::make_shared<actor>();
        actorListPtr.push_back(tmp);
    }

    static std::vector<shared_ptr<Actor>> & actorListPtr;
}

What I wish by this is to have a link to a separate vector, a vector with the same behavior for each individual instance of the class, referenced from another source. Any changes made in the source are to be reflected upon its pointer and vice versa. For instance.

std::vector<shared_ptr<Actor>> actorList;
Actor::actorListPtr = actorList;

Actor guy;
guy.add();

Should make the contents of actorListPtr equal to actorList. However, for me, this is not the case. What am I missing?

qwarten
  • 105
  • 2
  • 6

2 Answers2

2

std::make_shared<Actor>() makes a new Actor, but you probably want this. In any case, you can't create an owning shared_ptr to an arbitrary, pre-existing object which already has an owner. Try instead:

class Actor {
public:
    static Actor & make()
    {
        auto tmp = std::make_shared<Actor>();
        actorListPtr.push_back(tmp);
        return * tmp;
    }

    static std::vector<shared_ptr<Actor>> & actorListPtr;
}

Actor & guy = Actor::make();
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • Do you mind elaborating a bit on what you mean by your first statement? I get that it is like creating an actor with the ```new``` operator, but why is that an issue? – qwarten Feb 10 '17 at 07:50
  • @qwarten You're calling `guy.add()`, but `add` ignores `guy`. – Potatoswatter Feb 10 '17 at 07:53
1

What I wish by this is to have a link to a separate vector, a vector with the same behavior for each individual instance of the class, referenced from another source. Any changes made in the source are to be reflected upon its pointer and vice versa.

You seem to be treading on advanced C++ topics. There's a design pattern for this. It's called Dependency Injection.

Here's one way to make it work.

class Actor {
   public:
      void add()
      {
         auto tmp = std::make_shared<Actor>();
         if ( actorListPtr )
         {
            actorListPtr->push_back(tmp);
         }
      }

      // Allow client code to inject a pointer to be used on subsequent
      // calls to add().
      static void setActorList(std::vector<std::shared_ptr<Actor>>* newActorListPtr)
      {
         actorListPtr = newActorListPtr;
      }

   private:
      static std::vector<std::shared_ptr<Actor>>* actorListPtr;
}

// The static member variable is initialized to nullptr.
// Client code sets the value.
std::vector<std::shared_ptr<Actor>>* Actor::actorListPtr = nullptr;

// Client code.

std::vector<shared_ptr<Actor>> actorList;
Actor::setActorList(&actorList);

Actor guy;
guy.add();
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Your initialization of ```actorListPtr```gives me a compiler error, are you sure you don't mean ```std::vector> * Actor::actorListPtr;```? – qwarten Feb 10 '17 at 08:25
  • You'll need to use `std::shared_ptr`. I'll fix the answer later. – R Sahu Feb 10 '17 at 08:41
  • All is now working. I do not know the validity of my question but thanks for helping me as opposed to the other guys. – qwarten Feb 10 '17 at 13:51