2

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.

Louden100
  • 53
  • 2
  • 5
  • 1
    Do you want the vector to contain *copies* of the `Obstacle` objects, or pointers to them? I suspect the latter (since presumably you want to manipulate the original objects via the vector), in which case you'll need a vector of `Obstacle*`, not `Obstacle`. Also you probably want to remove elements from the vector in the `Obstacle` destructor. – j_random_hacker Nov 02 '12 at 13:42
  • j_random_hacker thanks, this is working now. – Louden100 Nov 02 '12 at 14:10

3 Answers3

2
  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);
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 1
    Although s/he doesn't say, I very much doubt the OP wants *copies* of the `Obstacle` objects in the vector. – j_random_hacker Nov 02 '12 at 13:40
  • I don't think he wants to store copies if he wants to be able to access all instances. Also, what would the copy constructor do? That should really add to the list as well but then you'd just get stuck in a recursive chain. –  Nov 02 '12 at 13:43
2

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);
}
1

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.

john
  • 7,897
  • 29
  • 27