2

I'm trying to override a method of the base class used by another method in the base class; however, when the derived class invokes the using-method of the base class, the derived used-method is never executed, but instead the base class's used-method is called. Here's an example:

#include <iostream>
using namespace std;
class Base {
public:
    Base() {}
    virtual ~Base() {}
    void printLeft() { cout << this->getLeft(); }
    int getLeft() { return 0; }
};

class Derived: public Base {
public:
    Derived() {}
    virtual ~Derived() {}
    int getLeft() { return 1; }
};
int main(int argc, char *argv[]) {
    Derived d = Derived();
    d.printLeft();
}

Running main() prints 0, indicating Base's getLeft() method was used rather than the derived object's method.

How can I change this code so Derived::getLeft() is called when called from an instance of Derived?

weberc2
  • 7,423
  • 4
  • 41
  • 57
  • Sorry, I don't have time. This project is due tomorrow. Thank you, however, for your helpful suggestion. – weberc2 May 15 '12 at 16:17
  • For the downvoter(s), is there something particularly wrong with the question? Should I clarify something? I tried researching, but as I don't know the terminology for the phenomenon I'm trying to describe, I wasn't able to find any pertinent results. – weberc2 May 15 '12 at 16:57
  • I downvoted this question, but I didn't do that until you told me that you were unwilling to read a book to learn C++. – John Dibling May 15 '12 at 17:50
  • Gotcha. I was concerned the downvote may have been in some way relevant. Glad to know it was only personal. Thank you for your honesty. – weberc2 May 15 '12 at 18:40
  • [It's not personal](http://meta.stackexchange.com/a/128589/142865). You can't learn C++ well by reading an online tutorial. You don't do your co-workers, your employers, your teachers or yourself any good by trying to take shortcuts and get spoon-feeding. – John Dibling May 15 '12 at 18:51
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11283/discussion-between-weberc2-and-john-dibling) – weberc2 May 15 '12 at 22:02
  • I accepted the invitation to chat, but weberc2 closed the chat before I had a chance to elaborate. Therefore, I will try to elaborate here. – John Dibling May 15 '12 at 22:22
  • This question is noise. A fundamental, beginner C++ question that was posted in an attempt to ask the internet instead of taking the time to sit down and really learn C++. I see no attempt by the poster to do their own research, which was confirmed when the poster refused my original suggestion. I did not d/v out of retailiation, as suggested by OP, but because this question adds to the noise on SO, which makes it a bad question. – John Dibling May 15 '12 at 22:28
  • Sorry, I honestly didn't mean to close the chat, nor did I realize I did. I'll try to open it back up again. Sincere apologies. – weberc2 May 16 '12 at 08:41

2 Answers2

6

You just need to make getLeft virtual:

class Base {
public:
    Base() {}
    virtual ~Base() {}
    void printLeft() { cout << this->getLeft(); }
    virtual int getLeft() { return 0; }
};

By default in C++ , member functions are not virtual. That is, you can't override them in a subclass.

Nick
  • 25,026
  • 7
  • 51
  • 83
  • 2
    Well, you *can* override them, it's just that the overriding will not have any effect when called through a base class pointer or reference. – JohnMcG May 15 '12 at 16:14
1

For non-virtual functions, the static type of the object is used to determine which class's method is called.

In your case, you are calling getLeft() from Base::printLeft(). The type of this is Base*, so the function called will be Base::getLeft().

The way around this is to use the virtual keyword. In that case, the virtual function table will be used to determine which version of getLeft() to call, in this case Derived.

You can trigger this behavior by prepending virtual to the declarations of printLeft in both Base and Derived.

JohnMcG
  • 8,709
  • 6
  • 42
  • 49