pure virtual function implemented without error

Jul 30, 2015 at 1:55pm
Hello, I created an abstract class with pure virtual function. If I understand correctly, there is no way to call/implement such function.
nevertheless, just out of curiosity I tried to implement it and everything compiled without any errors or warnings (I use VS 2012).
Why?
Jul 30, 2015 at 2:05pm
It is possible. Making the function pure virtual just makes the class abstract, and all derived non-abstract classes are forced to override the function.
Jul 30, 2015 at 2:06pm
ok, thanks man
Jul 30, 2015 at 3:36pm
It is possible to implement a pure virtual function. It might come handy when you have the same implementation for a function for all the derived classes.

for eg:

class A {
public:
virtual void foo() = 0;
}

void A::foo() { cout << "Pure" << endl; }

class B : public A{
public:
void foo() { A::foo(); }
}

class C : public A{
public:
void foo() { A::foo(); }
}

I have heard that you can also implement the function along with the pure specifier in Microsoft Visual C++. i.e.,

class A {
public:
void foo() { cout << "pure" << endl; } = 0
}

is valid in MS Visual C++.

But this is an error in other compilers and its better if you don't use it.
Topic archived. No new replies allowed.