I tried to write an pure virtual function in Base class, and i give it an body near the definition as it shown in the code bellow, as I know i should get an compile error, But everything worked fine. Is it something new that come with C++17 ? (I used visual studio 2017)
class Base {
public:
virtual void virtual_func() { std::cout << "This a virtual function from BASE" << std::endl; };
virtual void pure_func() = 0 { std::cout << "This a PURE virtual function from BASE" << std::endl; };
};
Thanks
Pure virtual functions (when we set = 0 ) can also have a function body.
A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class if the derived class is not abstract. Classes containing pure virtual methods are termed "abstract" and they cannot be instantiated directly.
¶ Δ A pure virtual function is a function that must be overridden in a derived class and need not be defined. A virtual function is declared to be “pure” using the curious =0 syntax.
A pure virtual function is a member function of base class whose only declaration is provided in base class and should be defined in derived class otherwise derived class also becomes abstract. Classes having virtual functions are not abstract. Base class containing pure virtual function becomes abstract.
The pure virtual cannot be used together with a definition. This was true in C++11, and in C++14:
10.4/2:...
[ Note: A function declaration cannot provide both a pure-specifier and a definition — end note ] [ Example:struct C { virtual void f() = 0 { }; // ill-formed };
The same statement (under section 13.4/2 this time) is still true for C++17 (N4659).
So if your compiler accept this code, it's certainly a bug (gcc 7.1 doesesn't)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With