The problem with virtual functions

I use native C ++ with VSTS 2008. Quick question about virtual function. In my example below, any differences if I declare Foo as "virtual void Foo ()" or "void Foo ()" in the Derived class? Any influence on any future classes that will be derived from the Derived class?

class Base { public: Base() { } virtual void Foo() { cout << "In base" << endl; } }; class Derived : public Base { public: Derived() { } void Foo() { cout << "In derived " << endl; } }; 
+4
source share
2 answers

No difference. But for the sake of flexibility, I always keep virtual whenever possible.

+9
source

No, as long as it has the same signature as the member function in the base class, it will be automatically made virtual. However, you must make this explicitly virtual in order to avoid confusion with code reading.

+4
source

Source: https://habr.com/ru/post/1303790/


All Articles