Sign in to follow this  
Gink

Array of Pointers?

Recommended Posts

Are array of pointers to classes used mainly to avoid the overhead of copying large class objects into data structures such as arrays / vectors, or is there another reason? What other areas would it be wise to use them in?

Share this post


Link to post
Share on other sites
Quote:
Original post by Gink
What other areas would it be wise to use them in?

Inheritance. If you have some interface exposed through a base class, a container full of pointers to that base class can have objects of any derived class in them. If you store actual objects, you lose all the derived functionality.

CM

Share this post


Link to post
Share on other sites
Quote:
Original post by Gink
So using actual objects is equivalent to downcasting a subclass to the superclass(all data from the subclass would be gone)?


Quick example:

class Parent
{
public:
void IAmTheParent() { }
};

class Child : public Parent
{
public:
void IAmTheChild() { }
};

Parent* ptr = new Child;
ptr->IAmTheParent(); // Works
ptr->IAmTheChild(); // Does not work, Child functionality removed

Child* temp = (Child*)ptr; // Dangerous doing it like this, but we know ptr is of the Child class
temp->IAmTheChild(); // Works fine
temp->IAmTheParent(); // Will also work because of inheritance




As for reasons to use this, I agree with CM. In a game you can have one abstract class that contains the basic functions all object should have in the game, Draw, Update, Destroy, and Create. Then you can simply make a vector of those objects and easily call the cycles of them. When you need specific objects, you can typecast based on a class ID that you can opt to add in. Neat stuff.

Share this post


Link to post
Share on other sites
Quote:
Original post by Drew_Benton
...

To expand on that a bit:

class Parent
{
public:
void IAmTheParent() { }
virtual void WhoAmI() {cout << "Parent" << endl;}
};

class Child : public Parent
{
public:
void IAmTheChild() { }
virtual void WhoAmI() {cout << "Child" << endl;}
};

Parent* ptr = new Child;
ptr->WhoAmI(); //outputs "Child"
delete ptr;
Parent parent = Child();
parent.WhoAmI(); //outputs "Parent"
Child child = Child();
child.WhoAmI(); //outputs "Child"
child = Parent(); //Compile error


The Parent object is not aware of the fact that it is supposed to be a Child. But the Parent* object is. So your question about downcasting is exactly right.

CM

Share this post


Link to post
Share on other sites
Here is one gameprogramming usage:


class GeneralMonster {..};
class Spider : public GeneralMonster{..};
class Dragon : public GeneralMonster{..};

#define numMonster 2

GeneralMonster* monsters[numMonster];

monsters[0] = new Spider();
monsters[1] = new Dragon();

for(int i=0;i<numMonster;i++)
{
monsters[i]->Update();
monsters[i]->Render();
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this