3

I was wondering if it's possible to override just one function in a class without creating an entirely new class.

I would like bObj1.foo(); to output "foo!" and bObj2.foo() to output "foo?", but currently they both output "foo!".

#include <iostream>
using namespace std;

class B {
    public:
    virtual void foo() { cout << "foo!" << endl; }
};

class A {
    public:
    B f();
};

B A::f() {
    B bObj;
    return bObj;
}

class C : public A {
};

int main()
{
    A aObj;
    B bObj1 = aObj.f();
    bObj1.foo(); // "foo!"

    C cObj;
    B bObj2 = cObj.f();
    bObj2.foo(); // "foo?"
}
Jeremiah
  • 512
  • 2
  • 5
  • 17
  • 1
    You want to return an object with a custom override of one of its virtual functions? That isn't really possible with virtual functions - that's stepping into function object territory. Can you give an example of how you'd ever use this? In your example, the word `foo?` doesn't exist. – Ayjay Oct 05 '11 at 05:27
  • Yes. I want to return a `B` that has its `foo()` overridden. – Jeremiah Oct 05 '11 at 05:31
  • The reason is that `A::` is really a linked list and `B::` is really an iterator. I am inheriting `A::` into `C::` and want different iterator behavior for `C::`. I am stuck with this design. – Jeremiah Oct 05 '11 at 05:34
  • Which of these classes are you allowed to modify? – Beta Oct 05 '11 at 05:49

2 Answers2

1

In order to change the virtual function, you have to create a new type - there's no way around that in C++. However, an alternate mechanism - function objects - may do what you want here.

#include <functional>
#include <iostream>

using namespace std;

class B {
public:
    B(function<void ()> foo_impl) : foo_impl(foo_impl) {}

    void foo() {foo_impl();}
private:
    function<void()> foo_impl;
};

class A {
public:
    virtual B f();
};

B A::f() {
    B bObj([](){cout << "foo!" << endl;});
    return bObj;
}

class C : public A {
public:
    virtual B f() override;
};

B C::f() {
    B bObj([](){cout << "foo?" << endl;});
    return bObj;
}

int main()
{
    A aObj;
    B bObj1 = aObj.f();
    bObj1.foo(); // "foo!"

    C cObj;
    B bObj2 = cObj.f();
    bObj2.foo(); // "foo?"
}
Ayjay
  • 3,413
  • 15
  • 20
1

You can get the behavior that you want with a simple change, which consists in moving the "virtual" behavior to the A and C classes.

Here I modified your application to return the expected result:

#include <iostream>
using namespace std;

class A;

class B {
public:
    B(A& a) : aref(a) {}
    void foo();
private:
    A& aref;
};

class A {
public:
    B f();
    virtual void foo() { cout << "foo!" << endl; }
};

B A::f() {
    B bObj(*this);
    return bObj;
}

class C : public A {
public:
    virtual void foo() { cout << "foo?" << endl; }
};

void B::foo() { aref.foo(); }

int main()
{
    A aObj;
    B bObj1 = aObj.f();
    bObj1.foo(); // "foo!"

    C cObj;
    B bObj2 = cObj.f();
    bObj2.foo(); // "foo?"
}
Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152