Jump to content
  • Advertisement
Sign in to follow this  
tscott1213

Static DataMember Question

This topic is 4787 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 all, I have a followup question to something I had asked about on this forum earlier... Given the following classes: class A { private: static int s_aField; public: static int getField() { return s_aField;} } class B : public A { private: static int s_aField; } My understanding of the above situation is that the s_aField defined in class B will hide the one defined in class A. Therefore, can I safely do the following to get the value of the static variable declared in class B: B* anInstance = new B; B->getField(); Do I need to declare the member function in class A as virtual to correctly retrieve the variable declared in class B in the following scenario: B* anInstance = new B; A* anotherInstance = B; anotherInstance->getField(); Finally, in this last case, I understand how virtual methods are handled in the case of an overridden member function (via a table of function pointers), but internally how does the language know which variable to return? Thanks Todd

Share this post


Link to post
Share on other sites
Advertisement
i dont think that will work for you

i have a feelin that declaring the finction as virtual

and making an overridden one in the derived class would work.

BTW, cant you just test that out for yourself and see the output?

[EDIT]

tested it and it uses the value in the BASE class

this is because the compiler doesnt know that there will be a base class
at compile time, and thus only access the static one defined in the base class

virtualising this method will do the trick

personally i dont like using things where the names hide each other,
but maybe thats becuse i haven't found a use for them

Share this post


Link to post
Share on other sites
Your assumption is wrong. getField() will always return A::s_aField. Even B->getField() returns A::s_aField. In order for B->getField() to return B::s_aField, you have to implement a B::getField() function.

In order for A->getField() to return B::s_aField you have to use virtual functions as you suspect.

Share this post


Link to post
Share on other sites
Thanks guys, I appreciate the help.

JohnBolton: You're right, I should have tested the cases myself before posting. Either way, I was going to come here looking for an explanation as to WHY it was doing what it does (But I still should have tested it first).

Thanks again.
Todd

Share this post


Link to post
Share on other sites
Yes, the right way to go here is to declare the getField() method in class A virtual and override it in class B.

Data members of classes cannot be virtual, only functions can. The getField() of class A will always return the s_aField of class A, and the getField() of class B will always return the s_aField of class B.

When you declare getField() of class A virtual, override it in B and make an instance of class B, then the getField() of class B will get called, no matter if the pointer to it is A* or B*. Therefore the returned value will always be the s_aField of class B.

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!