sub class uses base class virtual function help!

Started by
8 comments, last by rakoon2 19 years, 8 months ago
Hello! I have this problem again! If I try to use getMaxDmg(),.. with a CArrow_i object it uses the CItem function! Not CArrow_is! What do I wrong? :/ Thank you! :) Oh, and I can'T make the functions pure virtual!

   class CItem
   {
     public:
                  virtual int getMinDmg( void ){ return -1; };      
                  virtual int getMaxDmg( void ){ return -1; };      
                  virtual const int getMinDmg( void ) const{ return -1; };                                  
                  virtual const int getMaxDmg( void ) const{ return -1; };
     protected:
                 //..
};





class CArrow_i : public CItem
{
  public:
          int getMinDmg( void ) { LOG << "returnmin: " << min_dmg << ENDL; return min_dmg; }
          int getMaxDmg( void ) { LOG << "returnmax: " << max_dmg << ENDL; return max_dmg; }
          const int getMinDmg( void ) const{ LOG << "returnmin: " << min_dmg << ENDL; return min_dmg; }
          const int getMaxDmg( void ) const{ LOG << "returnmax: " << max_dmg << ENDL; retur
n max_dmg; }  
  
  private:
             int min_dmg;
             int max_dmg; 
};       



Advertisement
I remember I had something similar. Try adding "virtual" in front of the functions in your derived class(es).

Toolmaker

Quote:Original post by rakoon2
Oh, and I can'T make the functions pure virtual!


It sounds like you are using your classes incorrectly - making instances of the base class. You need to use pointers or references to the base class in order to get polymorphic behaviour.
i am using:

 CItem*  item;   item = new CArrow_i; delete item;

What are those extra semi-colons for?

virtual int getMinDmg( void ){ return -1; };      
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
If you aren't using instances of the base class anywhere, making the functions pure virtual shouldn't be a problem. The other problem may have something to do with the fact that you are trying to overload virtual functions (it's easy to hide functions by mistake).
Quote:Original post by Verg
What are those extra semi-colons for?

virtual int getMinDmg( void ){ return -1; };


mistake.. =P


Hmm.. I think I solved the problem now! :) BUt no idead how.. -_- thank you! :)

your code is correct and should work... and btw you don't need to return a constant variable for a constant method.


I even tried compiling your code and it works just fine here. vc7.1 ... is this another vc6 crapyness? O_O
Quote:Original post by Daher_Q3A
btw you don't need to return a constant variable for a constant method.


Yes this:
virtual int getMinDmg(void);virtual int getMaxDmg(void);virtual const int getMinDmg(void) const;virtual const int getMaxDmg(void) const;


is unnecessary only do this if your returning reference/pointer types not value types, this should be fine:

virtual int getMinDmg(void) const; virtual int getMaxDmg(void) const;
Oh! :-) Didn't know that! Thank you.

This topic is closed to new replies.

Advertisement