Advertisement

Virtual Functions and Casting

Started by January 25, 2001 02:10 AM
6 comments, last by JoeDark 24 years ago
Is there a way to cast an object to a derived class and call the derived classes virtual function? Like:
((derived *)base_pointer)->vfunc();  
In my program this still seems to call the base class's vfunc(). Is there another method of doing this? dynamic_cast? JoeDark Edited by - JoeDark on January 25, 2001 3:12:07 AM
JoeDark, if you call a virtual function it will always call the derived classes function, not the base. The problem is probably in your function definition. If you could paste your class definition so we could look at it...


- Houdini
- Houdini
Advertisement
You must make the function virtual, just put the word virtual in front of the function definition in the class-definition, like:
  class A{public:  virtual void MyFunc() {printf("A\n");}};class B : public A{public:  void MyFunc() {printf("B\n");} // don''t need virtual here since                                 // it was declared virtual in                                 // the base-class, it will                                  // always be virtual};  

BTW, just to add to what amag said, you don''t NEED the virtual keyword in your derived classes, but I would always recommend including it. Otherwise when your project gets larger you may not remember if a function is virtual in the class, and instead of just looking at the header you have to burrow down till you find your base class just to find out.

It''s also EXTREMELY helpful when editing someone elses code.


- Houdini
- Houdini
Here's how the classes are defined:

    //--------------------------------------------      class entity {   unsigned long int guid;   static unsigned long int guid_count;public:   entity()   {       guid = guid_count++;      next = NULL;      prev = NULL;   }         ~entity() {}   unsigned long int get_guid() { return(guid); }   entity *next;   entity *prev;     virtual void init_random() { log.print("Warning: Entity base init_random() called."); }   void init(int init_state)   {      switch(init_state)      {	 case I_RANDOM: init_random(); break;      }   }};//--------------------------------------------class platform : public entity, public weapon_holder{   long int x, y;   int owner;   unsigned long int parent;public:   void set_location(long int _x, long int _y) { x = _x; y = _y; }   void get_location(long int &_x, long int &_y) { _x = x; _y = y; }   void set_owner(int _owner) { owner = _owner; }   int get_owner() { return(owner); }   void set_parent(unsigned long int _guid) { parent = _guid; }   unsigned long int get_parent() { return(parent); }};//--------------------------------------------class starport : public platform{public:   virtual void init_random();};void starport::init_random(){   log.print("Starport: init_random");      // Set number and strength of weapons   gun_count = (rand() % 180) + 20;   laser_count = (rand() % 80) + 10;   launcher_count = (rand() % 15) + 5;   bay_count = (rand() % 4) + 1;      gun_damage = (rand() % 40) + 10;   gun_acc = (rand() % 60) - 10;   gun_rof = (rand() % 1) + 1;   laser_damage = (rand() % 40) + 30;   laser_acc = (rand() % 30) + 40;   laser_rof = 0.5;   launcher_damage = (rand() % 100) + 50;   launcher_acc = (rand() % 40) + 20;   launcher_rof = 0.2;   bay_rof = (rand() % 19) + 1;   // Create the weapons   int index;   for(index=0;index<gun_count;index++)      create_gun(get_guid());   for(index=0;index<laser_count;index++)      create_laser(get_guid());   for(index=0;index<launcher_count;index++)      create_launcher(get_guid());   for(index=0;index<bay_count;index++)      create_bay(get_guid());}        


And in one of my init functions I basically have:

        void Init_Universe(){   entity *e_pointer;   e_pointer = new entity;   e_pointer->init_random();   // ((starport *)e_pointer)->init_random();}    


And both of the calls to init_random() seem to call the base function.

JoeDark

Edited by - JoeDark on January 25, 2001 10:52:30 AM

Edited by - JoeDark on January 25, 2001 10:54:18 AM

Edited by - JoeDark on January 25, 2001 10:55:45 AM

Edited by - JoeDark on January 25, 2001 10:56:59 AM
For the derived class''s virtual function to be called, you must allocate a derived class instance, ie not
e_pointer = new entity;
but
e_pointer = new startport;

Then it should be ok.

belette/wipe
www.wipe-fr.org
Advertisement
AP is correct, but just to clarify so you aren''t confused:

entity *e_pointer;e_pointer = new entity;e_pointer->init_random(); 


should be

entity *e_pointer;e_pointer = new starport;e_pointer->init_random(); 


Notice that it''s still declared as ''entity *'' but is newed as ''starport''.


- Houdini

- Houdini
Thanks to everyone who replied. That helped clarify it.

JoeDark

This topic is closed to new replies.

Advertisement