Sign in to follow this  

Static methods and inheritance

This topic is 1959 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.

[CODE]
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();
};
[/CODE]

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
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:
[code]
class Derived : public Base<Derived> {
public:
using Base<Derived>::Make;
static Derived* Make();
};
[/code]
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

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