Jump to content
  • Advertisement
Sign in to follow this  
lonewolff

Class 'Virtual' question

This topic is 865 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

Hi guys,

With classes and how you can override 'virtual' members with your own implementation, is it possible to also override (or add additional) public & private variables to the new class instance also?

For example class instance A might just used the default public variables and class instance B might have additional variables X,Y, & Z.

Then you could query the value of X by doing Something = B.X

Is this sort of thing possible?

Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement

Yes you can!!


Indeed, that's a case of B hiding something it inherits from A. It's not good practice because it can be rather confusing. Some compilers will emit a warning.

Share this post


Link to post
Share on other sites

Thanks guys for information.

 

I have up-voted all answers and understand the pro's and (mainly) con's of doing such a thing now.

 

Thanks again :)

Share this post


Link to post
Share on other sites

please note that if you do this as Alberth did, and pass each instance of class B to a function that takes an instance of class A, it will use only the members in class A. For

example:

int GetZ(const A& a)
{
    return a.z;
}

int main(int argc, char **argv)
{
   A a;
   B b;
   a.z = 52;
   b = a;
   b.z = 1.5;
   std::cout << GetZ(b) << std::endl;
   return 0;
}

will always output "52".
So I'm not even sure that doing such a thing would serve a practical purpose.

However, you can add new members, and doing so is usually the purpose in making a new class. You just can't really override the meaning of old ones.
And if you need to change how certain members are used or accessed, you can use virtual functions for that.

Edited by nfries88

Share this post


Link to post
Share on other sites

Yep, that would be the plan. I wouldn't use class A in this case, it would always be a derivation. So, I'd only ever use class B, C, D, etc. :)

Share this post


Link to post
Share on other sites
Yep, that would be the plan. I wouldn't use class A in this case, it would always be a derivation. So, I'd only ever use class B, C, D, etc.

You mean you only instantiate derived classes, right?

(makese sense, as you generally don't code a derived class if you don't use it smile.png )

 

However, assuming your A class has a reason for existing (otherwise making separate classes B, C, D would be better), there is code that you (indirectly) use, which does use the A instance inside your derived class instances. Your sanity is a lot better preserved if you make the derived classes a particular form of the base class (which is what inheritance is about).

 

The "A::z" access in C++ mostly exists because Mr Stroustrup considers it important that you can access all variables at all times. Not because it is a useful thing to do here, but if you ever run into this problem, and you cannot fix it nicely, then, as a last resort, the C++ language does give you an escape route to work around the problem for now.

 

My example was very much constructed. In a real case, I would move the A::z code to another derived class, rename either A::z or B::z, or make A::z a real, so I wouldn't need B::z.

 

 

C++ is a lot about giving choice to the programmer. It doesn't try to decide for you what you should or should not do. It provides functionality, and it's up to you to draw a border what you find acceptable code.

 

@ExErvus: What is LSP?  Even Wikipedia doesn't know what it means: https://en.wikipedia.org/wiki/LSP

Edited by Alberth

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!