Louden100
Louden100

Reputation: 53

C++ Vector of classes, can't push_back inside constructor of said class

So here is my problem, I want a vector of objects of a certain class that I can then pass to other classes for use.

I want the Vector to add an instance of the class within the constructor of that class. So I've set up a static vector inside the class members and in the constructor I want to try and push_back(this) if that makes any sense.

There are 2 classes currently at work, one is the event handler class that creates an instance of "Obstacle" class (the class I want a vector of) every time there is a left click.

(I have omitted irrelevant bits of code)

class MyEventReceiver: public IEventReceiver
{
private:
    blah blah


public:
    blah blah

virtual bool OnEvent (const SEvent& event){
    if(left click)
       Obstacle obs(scn,randX,randY,randR);
}

}

Obstacle.h:

class Obstacle
{
private:

    static std::vector<Obstacle> *obs;

public:

    Obstacle(scene::ISceneManager*, float, float, float);
    ~Obstacle();
};

Obstacle cpp:

std::vector<Obstacle> *Obstacle::obs;

Obstacle::Obstacle(scene::ISceneManager* scn, float x, float y, float radius)
{
    //get and set the passed in params blah blah

    obs->push_back(this);


}

This doesn't work, but hopefully you can see what I'm trying to achieve. Later on I want a function that will return a reference to obs so other classes can call this function and gain access to the vector to manipulate and read it.

Upvotes: 2

Views: 5048

Answers (3)

user213313
user213313

Reputation:

You really want to change your vector so that you store them by pointer and not by value as you don't want separate copies in there:

static std::vector<Obstacle*> *obs;

Remember to remove your instance from this vector in your destructor:

Obstacle::~Obstacle()
{
    auto iter = std::find(obj->begin(), obj->end(), this);
    if (iter != obj->end())
    {
        obj->erase(iter);
    }
}

You also need to write a copy constructor which adds it as well. If you don't do this then you won't be able to track copies (assuming you allow this):

Obstacle::Obstacle(const Obstacle& rhs)
    // ... initialise from rhs
{
    obs->push_back(this);
}

Upvotes: 2

john
john

Reputation: 8027

Your vector is declared wrong. It should be

static std::vector<Obstacle*> obs;

i.e. a vector of pointers. What you had was a pointer to a vector.

Assuming that your goal is to maintain a vector of pointers to all constructed Obstacle objects, then you are also going to have to declare the copy constructor (copied Obstacle objects will also need adding to your vector) and the destructor (which should remove objects from the vector). This won't be efficient but it may be OK for your needs.

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258558

  1. Don't have obs as a pointer, there's no need for that.
  2. obs is declared as a vector of objects. this is a pointer to an object, so you'll have to call push_back as obs->push_back(*this);

Upvotes: 2

Related Questions