Jump to content
  • Advertisement
Sign in to follow this  
Strewya

Static methods and inheritance

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

i ran into something i thought would work, but doesn't seem it does, so an explanation would be welcome.


template<class T>
class Base
{
public:
static T* Make(ticpp::Element* xml);

class Maker
{
public:
T* MakeObject(ticpp::Element* xml) { return T::Make(xml); }
};
};

class Derived : public Base<Derived>
{
public:
static Derived* Make();
};


this code gives me the error that the function Derived::Make does not take 1 argument. some other static methods i have in Base<Derived> are accessible through Derived, but not Make(xml), only Make(). i'm guessing it has something to do with the fact they have the same name in both Derived and Base, but i thought they would just work like overloaded methods.

does anyone know why this doesn't work?

Share this post


Link to post
Share on other sites
Advertisement
When you create a derived class any members with the same name as a name in the base class hides the name in the base class. Trying to access the name "Make" in the derived class won't find the version that accepts one argument because the no argument version hides the base class version. In order to make the base class version visible in the derived class you can use a using statement in the derived class. Ex:

class Derived : public Base<Derived> {
public:
using Base<Derived>::Make;
static Derived* Make();
};

Putting virtual in the base class definition wouldn't work because virtual and static are mutually exclusive. Even if it was a non-static method, the two functions have different argument lists so the derived class version would still hide the base class version.

Share this post


Link to post
Share on other sites
what if the base class has more than one version of Make(), would the using statement allow me to use all of them from the derived class?

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!