How to have two pure virtual methods with the same name but different return types

use of pure virtual function in c++
pure virtual function in c++ with example program
c++ virtual function = 0
c++ override
virtual destructor c++
c++ implement pure virtual function
can pure virtual function have body c++
abstract class in c++

I've got an interface A which is publicly available, and a back-end implementation class C, like so:

struct A
{
    virtual void f() = 0;
};

struct C : public A
{
    virtual void f() override
    {
    }
};

I would like to gradually migrate to a new interface, B. B will have the same methods, however, the return types might be different. I would like my implemetation class to implement both A and B so that legacy code can keep using A until it is migrated, but new code can be written with B. I know that if I have two pure virtual methods with exactly the same signature, I only need one override in my implementation class. But if they differ by return type, I don't know how to do it...

struct A
{
    virtual void f() = 0;
};

struct B
{
    virtual int f() = 0;
};


struct C : public A, public B
{
    // How do I implement both versions of f() here??
};

The way I would do it is

struct A {}
struct B {}
struct OldImpl : public A {}
struct NewImpl : public B {}
struct Combined : public AImpl, public BImpl {}

You can then pass the combined into anything that wants an A or a B. The problem with this is that your OldImpl and NewImpl are completely separate. They're not allowed to interact with each other; meaning you can't call setFoo() in A and get the same value in getFoo() in B. Which is probably not what you want from what you described.

To resolve this, don't create a 2nd interface. It wouldn't work anyway because where a xxx() might be void now, and an int later, you probably want to do something with the return value. If you really want to do it in baby steps, create a branch, change a single functions return value, test it, merge it in, repeat.

12.6, 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. class Base. {. public: const char* sayHi() { return "Hi"; } // a We've prevented people from allocating objects of type Animal by making the A pure virtual function is useful when we have a function that we want to put in the different. How is that possible for those two functions stored in the same  A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have implementation, we only declare it. A pure virtual function is declared by assigning 0 in declaration. See the following example. // An abstract class. // Data members of class. // Pure Virtual Function. virtual void show () = 0;


Thanks for the good responses! But I did figure out my own solution: good old optional parameters!

struct A
{
    virtual void f() = 0;
};

struct B
{
    virtual int f(int ignoreMe = 0) = 0;
};


struct C : public A, public B
{
    virtual void f() override {}
    virtual int f(int) override {}
};

No ambiguity for external users using A or B, and no parameters need to change. No ambiguity internally either because parameter is non-optional in the implementation class. The optional parameter can be removed, along with A, once migration is complete.

12.2a, note use of final specifier on following line -- that makes this function no longer overridable from a class, the final specifier is applied after the class name: a derived class virtual function override can have a different return type than the If the return type of a virtual function is a pointer or a reference to a  A virtual function is a member function in base class that you expect to redefine in derived classes. Before going into detail, let's build an intuition on why virtual functions are needed in the first place. An Example to Begin With. Let us assume, we are working on a game (weapons specifically). We created the Weapon class and derived two


This at least compiles with VS2017:

struct A
{
    virtual void f() = 0;
};

struct A2: public A
{
    void f() { Af(); }
    virtual void Af() = 0;
};

struct B
{
    virtual int f() = 0;
};

struct B2 : public B
{
    int f() { return Bf(); }
    virtual int Bf() = 0;
};

struct C : public A2, public B2
{
    void Af() {}
    int Bf() { return 42; }
};

Edit: Could not add this a comment on Paul Accisano's own answer, so I put it here.

Modifying Paul's answer slightly. By making the unused default parameter a protected class, you still have correct type checking, and avoid the potential for a type conflict with another member function.

struct A
{
    virtual void f() = 0;
};

struct B
{
protected:
    class disambiguator {};
public:
    virtual int f(disambiguator ignoreMe = disambiguator()) = 0;
};


struct C : public A, public B
{
    virtual void f() override {}
    virtual int f(disambiguator ignoreMe) override { return 42; }
};

A *createA() { return new C; }
B *createB() { return new C; }

One application for this could be the gradual migration from raw pointers to smart pointers.

Inheritance, What's the difference between how virtual and non- virtual member functions are called? Seems like this ought to use dynamic binding but can't figure it out. Thus there are two types: the (static) type of the pointer ( Vehicle , in this case), and A pure virtual function is a function that must be overridden in a derived class  The pure virtual function has no definition in the base class, and all the inheriting derived classes has to redefine it. However, the virtual function is also called as dynamic dispatch and run-time dispatch, due to the function called is specified in the run time in accordance with the type of the object used.


Inheritance, When my base class's constructor calls a virtual function on its this object, why doesn't my member function would necessarily have to be virtual (probably pure virtual ) but Yes, there really are two different basic ways to use virtual functions: a member function f(char c) (same name but different parameter types and/or  A virtual method is a method that can be redefined in derived classes. A virtual method has an implementation in a base class as well as derived the class. It is used when a method's basic functionality is the same but sometimes more functionality is needed in the derived class.


Accelerated C# 2005, You can code an efficient Add () method to a container type using parameter arrays point, the compiler chooses a method from a set of methods with the same name. within an overloaded class where the only difference is the return type. C++ pure virtual methods may have an implementation associated with them,  Rules for Virtual Functions. Virtual functions cannot be static and also cannot be a friend function of another class. Virtual functions should be accessed using pointer or reference of base class type to achieve run time polymorphism. The prototype of virtual functions should be same in base as well as derived class.


C# Primer Plus, By pure coincidence they have in this version added a virtual method to the the same name, return type and parameter types as your MoveForward method in in Java and the two MoveForward methods contained different return types the  And even if methods owned by the base class call the virtual method, they will instead be calling the derived method. Overloading occurs when two or more methods in one class have the same method name but different parameters. Overriding means having two methods with the same method name and parameters. Overloading is also referred to as dynamic function mapping and overriding as function matching.