Sign in to follow this  

sub class uses base class virtual function help!

This topic is 4869 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; 
};       



Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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! :)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites

This topic is 4869 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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