Base class calls method from derived class?

user3516422
class BaseClass {
 public:
  virtual void method1(){
   method2();
  };
  virtual void method2(){
   std::cout << "Base Method" << std::endl;
  };
}

class DerivedClass : public BaseClass {
 virtual void method2(){
   std::cout << "Derived Method" << std::endl;
  };
}

int main() {
  DerivedClass derived;
  derived.method1();
}

In the above example I get "Derived Method" as the output - Why does this happen?

I understand that DerivedClass inherits from BaseClass, and therefore derived can call method1, but I don't understand why method2 from DerivedClass hides method2 from BassClass when it is being called from BaseClass.

Apologies for any bad code/mistakes - still new to C++.

John Dibling

Because method2 is virtual.

When you declare a function as virtual, what you're really doing is making it so that when the method is called via a pointer or reference (in other words, in a normal way) the function that is actually called is the most-derived overload.

This is a Good Thing, and usually exactly what you want. Note that it doesn't matter from what context you make the call. Your'e calling method2 from the context of the base class, which has an implementation of method2 available. Presumably, you're assuming that since your calling from the base class that the base class' implementation is the one that will be called.

That's not how virtuals work -- and that's also a Good Thing.

You can, if you wish, call the version in the base class by being explicit about it:

class BaseClass {
 public:
  virtual void method1(){
   BaseCLass::method2();
  };

But this is usually not desirable, and in my book, a code smell.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Clone derived class from base class method

From Dev

Clone derived class from base class method

From Dev

Method called in creator is called from base class but not from derived class

From Dev

Abstract parent class calls derived class method

From Dev

Derived class not inheriting overloaded method from base class

From Dev

Calling derived class method from base class pointer

From Dev

Call derived class method from base class instance

From Dev

How to call derived class method from base class pointer?

From Dev

friendship from derived class method to base class members

From Dev

Access to base class method only from the derived class

From Dev

Calling derived class method from base class destructor

From Dev

Method for any class derived from a generic base class

From Dev

Calling derived class method from base class destructor

From Dev

Call derived class method from base class instance

From Dev

Using Derived Class In a Base Method

From Dev

Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

From Dev

Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

From Dev

Method in base class that returns derived class type?

From Dev

Derived class does not call base class method

From Dev

Derived class calls parent's method

From Dev

Calling a private base method from a derived class in C#

From Dev

Calling virtual method of base template from derived variadic template class

From Dev

Can I call a derived method from base class?

From Dev

returning derived class from overrided method, declared as returning base copy

From Dev

How to obtain the derived class type from base when calling a method

From Dev

Returning any derived class type from a method outside the base and derived class

From Dev

Creating a derived class from a base

From Dev

Why does calling a method from base class calls the child method?

From Dev

Create a base class object from a derived class

Related Related

  1. 1

    Clone derived class from base class method

  2. 2

    Clone derived class from base class method

  3. 3

    Method called in creator is called from base class but not from derived class

  4. 4

    Abstract parent class calls derived class method

  5. 5

    Derived class not inheriting overloaded method from base class

  6. 6

    Calling derived class method from base class pointer

  7. 7

    Call derived class method from base class instance

  8. 8

    How to call derived class method from base class pointer?

  9. 9

    friendship from derived class method to base class members

  10. 10

    Access to base class method only from the derived class

  11. 11

    Calling derived class method from base class destructor

  12. 12

    Method for any class derived from a generic base class

  13. 13

    Calling derived class method from base class destructor

  14. 14

    Call derived class method from base class instance

  15. 15

    Using Derived Class In a Base Method

  16. 16

    Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

  17. 17

    Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

  18. 18

    Method in base class that returns derived class type?

  19. 19

    Derived class does not call base class method

  20. 20

    Derived class calls parent's method

  21. 21

    Calling a private base method from a derived class in C#

  22. 22

    Calling virtual method of base template from derived variadic template class

  23. 23

    Can I call a derived method from base class?

  24. 24

    returning derived class from overrided method, declared as returning base copy

  25. 25

    How to obtain the derived class type from base when calling a method

  26. 26

    Returning any derived class type from a method outside the base and derived class

  27. 27

    Creating a derived class from a base

  28. 28

    Why does calling a method from base class calls the child method?

  29. 29

    Create a base class object from a derived class

HotTag

Archive