1

I'm reading about virtual functions and now little confused about overriding virtual functions. I want to confirm that below given codes are same?

class A{
public:
    virtual void fun(){ }
};
class B :public A{
public:
    void fun(){}
};

and

class A{
public:
    virtual void fun(){ }
};
class B :public A{
public:
    virtual void fun(){}
};

If not same then what's the difference? As I expected that B's function with virtual keyword could be same as for B's Derived. Please clear my confusion thanks.

ixSci
  • 13,100
  • 5
  • 45
  • 79
  • 2
    Both of those are equivalent. In C++11, you can use `void fun() override {}` in `B` to indicate that it is a virtual function inherited from the base class. – Mats Petersson Jul 11 '15 at 07:58

3 Answers3

2

Yes, they are the same. Any function overriding a virtual function in the base class is implicitly declared virtual.

From the last working draft of the c++14 standard:

10.3 Virtual functions [class.virtual]
If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name, parameter-type-list (8.3.5), cv-qualification, and ref- § 10.3 249 c ISO/IEC N4296 qualifier (or absence of same) as Base::vf is declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides(111) Base::vf.

Emphasis mine

As @Mats and @ixSci pointed out, it is good practice since c++11, to use the keyword override to ensure that you are actually overriding a virtual function and not acidentally overloading a function or overriding a non-virtual function. Personally, my preferred style is this, but it is up to debate whether the virtual keyword in B adds any value or is even harming readability:

class A{
public:
    virtual void fun(){ }
};

class B :public A{
public:
    virtual void fun() override {}
};
MikeMB
  • 20,029
  • 9
  • 57
  • 102
  • i want to confirm that I should override Base's function with same parameter list? I know overriding required same signature with parameter list and return type. But when I use pointer and doing this `void fun(int){}` in DerivedClass and then calling using `a->fun(3);` its showing error. But without pointer `DerivedObj.fun(3);` it's working fine. –  Jul 11 '15 at 08:02
  • 1
    Yes, you have to use the same parameter list, otherwise, you don't *override*, but *overload*. A typical gotcha concerning this is a virtual assignment operator, where the compiler generateed assignement operators of the drived class will not overrride the base class ones. – MikeMB Jul 11 '15 at 08:10
  • Just in case of Base's class type Pointer that point to derived class object? –  Jul 11 '15 at 08:14
  • @LetDoit: I think I don't quite understand what you mean. Maybe you can extend your question with a complete example of what is not working. If your question is about when/whether you should overload / override / virtual override at all, then this is a much broader topic which highly depends on the topic. – MikeMB Jul 11 '15 at 08:18
  • check this http://codepaste.net/e6hv3n It's showing error, But when I change `a->fun(3);` to `obj.fun(3);` then it works fine. –  Jul 11 '15 at 08:29
  • Ok, so in this example, you are overloading `fun` in the derived class. A pointer to base class however only gives you access to functions already declared in the base class. Overload resolution (different signature) is a *compile time processs*, so it is based on the static type of the pointer. If that function is a virtual function, then dynamic dispatch **might** come into play, meaning, that the compiler generates code, that is executed at *runtime* to determine the correct overlride based on the actual dynamic type of the object. – MikeMB Jul 11 '15 at 08:41
  • thanks for your effort to help me. Can you give me any link that explain how virtual works? as in my example. –  Jul 11 '15 at 08:59
  • @let: Sorry,not offhand, but have you srarched SO for similar questions? – MikeMB Jul 11 '15 at 09:47
0

virtual keyword on the overridden function is completely useless. It doesn't provide anything except readability(some might say it harms readability) but it was the only way in C++03 to convey to the class readers that the function is actually virtual without them checking the base class.

Nowadays it is better to use the keyword which is intruduced specifically for the overriding - override. So your example will look like:

class A{
public:
    virtual void fun(){ }
};
class B :public A{
public:
    void fun() override{}
};

Not only does it convey the intent but it also makes sure you won't make any mistake when overriding a virtual function.

ixSci
  • 13,100
  • 5
  • 45
  • 79
0

Virtual keyword tells compiler that there function may be implemented later by inheriting class ( pure virtual). Additionally in c++11 there is keyword override wich checks if implemented method in inheriting class overrodes virtual method of base class. In case of implemented function there is no much difference. We can sam that in case of virtual we talk avout override in other case about hiding of base method ( in both cases your cab still invoke implementation of base). Additionally this is information for programmer that function is intended to override. Additionaly virtual keyword is important in case of destructor. As general rule of thumb if function has some virtual methods it is good to have it a virtual destructor.

Robert Wadowski
  • 1,407
  • 1
  • 11
  • 14