Overriding base class method

Hi, I have a query with regards to the use of the Virtual keyword for methods.

If a derived class method overrides a virtual method of it’s base class,
and the Virtual keyword is also used in the .h method of the derived class
Then it seems to call the overridden method of the derived class if
the object type is of the base class. I’m not sure how that is possible as I would have expected it to call the base class method in such cases, but does happen. Now if I remove the virtual keyword from the .h of the derived class then the base class method is called instead.

I did not think there should be any behaviour difference if the virtual keyword was used or not used in the .h method of the derived class that overrides the same method name of the base class.



Last edited on
Jezck wrote:
Hi, I have a query with regards to the use of the Virtual keyword for methods.

If a derived class overrides a virtual method of a base class,
and the Virtual keyword is also used i the .h of the derived class
Then it seems to call the overridden method of the derived class even if
The object type is of the base class. I’m not sure how that is possible but does happen. Now if I remove the virtual keyword from the .h of the derived class then the base class method is called instead of the object calling the method is of the base class type.

I did not think there should be any behaviour difference if the virtual keyword was used or not used in the .h of the derived class.


There is no difference, see below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

struct base { virtual void f() const { std::cout << "base::f\n"; } };
struct derived0: public base { virtual void f() const override { std::cout << "derived0::f\n"; } };
struct derived1: public base { /* virtual */ void f() const override { std::cout << "derived1::f\n"; } };

int main()
{
  base const b;
  
  derived0 const d0;
  base const& rd0 = d0;
  
  derived1 const d1;
  base const& rd1 = d1;

  b.f(); // dynamic type of `b` is `base const`, this prints "base::f"
  d0.f(); // dynamic type of `d0` is `derived0 const`, this prints "derived0::f"
  rd0.f(); // dynamic type of `rd0` is `derived0 const`, this prints "derived0::f"
  
  d1.f(); // dynamic type of `d1` is `derived1 const`, this prints "derived1::f"
  rd1.f(); // dynamic type of `rd1` is `derived1 const`, this prints "derived1::f"
}
Thanks, that is what I see also. Except in the case where the parameter of the method takes in the base class type and the object being passed to it is of the derived type then within that method I can see the derived method is called but only if virtual is used for the derived method which overrides the base class method. If I remove the virtual keyword then the base class method is called so the behaviour is different in this case.

That is what I am seeing.
Thanks, that is what I see also. Except in the case where the parameter of the method takes in the base class type and the object being passed to it is of the derived type then within that method I can see the derived method is called but only if virtual is used for the derived method which overrides the base class method. If I remove the virtual keyword then the base class method is called so the behaviour is different in this case.

This isn't specific enough for me to fully understand the situation.
Please post a short program whose behavior deviates from expected.
Registered users can post here. Sign in or register to post.