Yola
Yola

Reputation: 19063

Conceal specific function from base class in derived class interface

struct IA 
{
    virtual void Init() = 0;
    .....
};

struct A : public IA
{
    void Init() {};
    .....
};

struct B : public A
{
    int Init() { return 1; };
};

With such design i got error C2555: 'B::Init': overriding virtual function return type ...

Can i somehow conceal Init() from A, i don't want to conceal other A's functions. Class A used from other places as A class not only through B class.

EDIT: I need to have two Init functions in the hierarchy with only difference in return types. I don't need A::Init to be called on objects of B type. Actually i can do it by

struct B : private A
{
    using A::.... // all, except Init
    int Init() { return 1; };
};

But there are a big lot of functions in A:(

Upvotes: 0

Views: 137

Answers (1)

shibumi
shibumi

Reputation: 378

Due to inheritance, your struct B contains both the function signatures void Init(); and int Init(); and C++ does not allow overloading methods which differ only in their return types.

Possible inelegant solutions:

  • You can fix this error by declaring your the void Init(); method in struct A as private and keeping the rest of the methods that you would like to inherit as public.
  • Another fix would be to add a dummy parameter such as bool and call the method with Init(true). Note that you cannot define a default value for this dummy parameter, else you would end up with the same error.

Upvotes: 1

Related Questions