If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Either that or you must allocate your pointer with "new". Either that or if your intent is to really have a pointer to the passed in array, you could do "animals = &animals;" if your animals pointer is also declared as const.

There are tons of tutorials, books and faqs that will set you straight on pointers and all these other basics that you're having trouble with. Asking questions like this is a horrible way to learn C++, and will take much much longer. The reason is you are only asking questions when you run into a problem. There are more important things that you don't even know HOW to ask.

For instance, in your example above, you now have two vectors pointing at the same allocated memory. Who owns that memory? Who is responsible for deleting it? There's a very elegant solution involving smart pointers to handle the memory for you, but I won't even suggest it before you get a better handle on the basics.

Well, jfaust already answered this, but since I wrote something up ...

Let's look at a simpler case ...

Code:

int i = 10;
int* pi;
pi = &i; // points pi to a valid int

so "i" is an int and "pi" is a pointer to int. Currently, pi points to "i".

Code:

*pi = 2;

changes the int variable pointed to by pi to 2 ("i" in this case).

Now look at this :

Code:

int* pi;
*pi = 2; // dereferences the pointer

pi does not point to a valid int variable, so deferencing the pointer
is undefined (basically means you don't know what will happen).

This is basically what you are doing ...

Code:

vector<Animal*> *animals;
*animals = anims;

animals is a pointer to a vector<Animal*>, but it doesn't point to anything.
*animals dereferences the pointer.

There are many ways to fix this depending on what you want to do (for instance,
is House going to be able to modify the animals vector ?). Here are a few. Note:
in the House constructor, I set the animals variable in the initialization
list instead of the body of the constructor. Its usually a good idea to do
that when you can (and sometimes you must do that).

Also note, in the code above that kept animals as a pointer, I
just set the pointer (no new as mentioned by jfaust). In this
case, the vector that was passed to the House constructor
must remain valid as long as the House variable is valid.

You look like you are going to run into more problems, as you have a vector of pointers, and you are going to copy this into another vector of the same pointers.

Which vector is going to delete the pointers?

Memory management is a complex problem, which is why many new languages (Java and C#) have taken it out of the equation now and handle it for you. So in Java you have to call new but you don't have to call delete.

Another issue, having handled all that, is that you will have a vector of pointers to class "animal" which is a base class, and you will be having pointers to derived classes, so you need to ensure that class animal has a virtual destructor.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.