0

I weren't surprised when found that I can use base-class pointers for usage to derived classes like this (very simple):

class Base
{
    protected:
    int m_value;

    public:
    Base(int value)
        : m_value(value)
    {
    }

    const char* getName() { return "Base"; }
    int getValue() { return m_value; }
};

class Derived: public Base
{
    public:
    Derived(int value)
        : Base(value)
    {
    }

    const char* getName() { return "Derived"; }
    int getValueDoubled() { return m_value * 2; }
};

What is happened after an object derived class has been created? If I use

Base &rBase = derived;

I'll use such methods that is determined in base class. Why? Does it mean that derived class actually consists first-determined getName() of Base class, but for now uses only overloaded method and if we want then we can call origin method by pointer on base class? Explain me please in terms of pointer and addresses.

user4581301
  • 33,082
  • 7
  • 33
  • 54
DisplayName
  • 219
  • 4
  • 23
  • 3
    It doesn't look like you're actually using any pointers here (which would allow you to call derived functions if you use polymorphism with `virtual`). In addition to that, I'd strongly advice using `std::string` over `char *` anywhere you can. – scohe001 Apr 09 '19 at 19:16
  • 3
    [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Jesper Juhl Apr 09 '19 at 19:17
  • Can't be explained that way, unfortunately, for the general case. Exactly how polymorphism works isn't defined by the C++ Standard. Only what must happen is defined, so the implementer could go off on wild flights of fancy so long as their solution provides the correct results. Usually the implementation will use what's known as (incoming search term!) [V-Tables](https://stackoverflow.com/questions/3554909/what-is-vtable-in-c) but they do not have to. – user4581301 Apr 09 '19 at 19:55
  • @user4581301 There is no polymorphism in OP's classes. – Daniel Langr Apr 09 '19 at 19:56
  • Irrelevant to my point. You can't explain polymorphism in terms of pointers and addresses because someone who's figured out how to use fairy dust has no need of such crude tools. – user4581301 Apr 09 '19 at 20:00

1 Answers1

3

In C++, member functions are not virtual unless declared so.

That means, that in your case if you call rBase.getName(), as the type of rBase is Base&, the function Base::getName() will be called, regardless of what derived type the object rBase refers to has.

If you want dynamic dispatch, you must declare the function virtual

virtual const char* getName() { return "Base"; }

With that, the call rBase.getName(), will look at the actual type of the object referenced by rBase, and -- if that is Derived -- call Derived::getName().

BTW, when you want to override a virtual function, use the override keyword. Then the compiler can help you find mistakes. In your case, had you used that in Derived, as such:

const char* getName() override { return "Derived"; }

you would have gotten an error like

 error: only virtual member functions can be marked 'override'

And, please heed the advice given by @scohe001 and use std::string instead of char* unless you have a good reason for needing a C-style string.

drRobertz
  • 3,490
  • 1
  • 12
  • 23