Archived

This topic is now archived and is closed to further replies.

Peon

More questions regarding object handlers

Recommended Posts

Peon    276
A while ago, I posted a question about object handler often used in object oriented programming. I managed to get an array of CEntity pointers, and add other classes to it, that were inherited from it.
          
CEntity* AllEntities[10];
AllEntities[0] = new CShip;  
Now my question is, whether or not it is possible to access the member functions of my CShip class from this array. What I want to be able to do is have an array of CEntity pointers, set them to a specific object (CShip, for instance), and use functions found in the CShip, or other objects:
            
CEntity* AllEntities[10];

//CShip and CMissile inherit from CEntity

AllEntities[0] = new CShip;
AllEntities[1] = new CMissile;

//this is what I want to be able to do

AllEntities[0].FireMissle()
AllEnitiies[1].Explode()
          
... but when I do this, I get a message such as
c:\developer\pixel\pixelmanager.h(215) : error C2039: 'PushVertices' : is not a member of 'CEntity'
        c:\developer\pixel\pixelentity.h(6) : see declaration of 'CEntity'     
I understand the error, but I'm not really sure how to implement my idea, assuming it's even _possible_ Any ideas on how I can do what I described above? Am I going about it the wrong way? Thanks in advance Peon [edited by - Peon on April 29, 2003 5:57:53 PM]

Share this post


Link to post
Share on other sites
Monder    993
You can use casting to do what you want. For example

(CShip)AllEntities[0]->FireMissile()

Though I''m not quite sure that''ll work. You can also do

CShip* Ship = (CShip)AllEntities[0];
Ship->FireMissile( );

Oh you''re not meant to use . when accessing classes via pointers you have to use -> (as in my examples).

Share this post


Link to post
Share on other sites