[Solved] Creating Array of Objects c++


C++ does not have an associated length property for arrays. (See How do I find the length of an array?)

You can do something like this:

int length = sizeof(animals)/sizeof(animals[0]);

Then, before accessing member variables of elements (like this: animals[i] -> hasFur(true);), you need to first create objects.

animals[i] = new Animal(); // assuming the constructor has no arguments

[Edit: not necessarily, see this answer]

Also, Amimal -> incCount() looks a bad idea to me. You can increment the counter in your constructor. This way ensures that you don’t accidentally call incCount() and mess up the count of your static variable.

7

solved Creating Array of Objects c++