Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading virtual functions of the same name from different base classes. Is it possible? [duplicate]

The title is probably confusing.

Suppose we have the following set up;

class A
{
public:
    virtual void fn() = 0;
};

class B
{
public:
    virtual int fn() {};
};

class C: public A, public B
{
};

Is there any way to define A::fn in class C?

like image 727
eugene2k Avatar asked Sep 06 '11 11:09

eugene2k


People also ask

Can virtual functions be overloaded?

It is not possible for these functions to get overloaded.

Is possible when a derived class function has the same name and signature as its base class?

In C++, function overloading is possible i.e., two or more functions from the same class can have the same name but different parameters. However, if a derived class redefines the base class member method then all the base class methods with the same name become hidden in the derived class.

Can a virtual function be friend of another class?

A virtual function can be a friend function of another class. Virtual functions should be accessed using pointer or reference of base class type to achieve runtime polymorphism. The prototype of virtual functions should be the same in the base as well as derived class.

Is virtual function and function overriding same?

The virtual keyword can be used when declaring overriding functions in a derived class, but it is unnecessary; overrides of virtual functions are always virtual. Virtual functions in a base class must be defined unless they are declared using the pure-specifier.


3 Answers

There's no way in C to specify that one of the C::fn() implementations overloads A::fn() (and presumably another overloads B::fn()). What you can do, however, is introduce an intermediate class which “renames” the functions, something like:

class RemapA : public A
{
    virtual void fnInA() = 0;
public:
    virtual void fn()
    {
        fnInA();
    }
};

class RemapB : public B
{
    virtual int fnInB() = 0;
public:
    virtual int fn()
    {
        return fnInB();
    }
};

class C : public RemapA, public RemapB
{
    virtual void fnInA() { /* ... */ }
    virtual void fnInB() { /* ... */ }
    //  ...
};
like image 76
James Kanze Avatar answered Oct 25 '22 21:10

James Kanze


No. This is not possible. It will always conflict with either of the fn().

The syntax of fn() are different,

void fn();  // in A

and in B is,

int fn();  // in B

You have to make those syntax same in A and B to let C implement the fn(). Demo.

like image 42
iammilind Avatar answered Oct 25 '22 22:10

iammilind


You might want to read my answer to the following question: Implement two functions with the same name but different, non-covariant return types due to multiple abstract base classes In short: Yes, with some restrictions on calling it. It can be called as an A (pointer or reference) or a B (pointer or reference), but not as a C, as that would be ambiguous.

like image 29
Tar Avatar answered Oct 25 '22 21:10

Tar