Mengyang Liu
Mengyang Liu

Reputation: 11

Pure virtual function

I know in C++, virtual double f()=0; is a pure virtual function, what about virtual void f() {return 0.0};? Is this a pure virtual function?

Upvotes: 1

Views: 737

Answers (2)

user394010
user394010

Reputation: 374

Pure virtual function is function marked with =0. It implicitly makes the class abstract. Abstract classes cannot be instantiated. Derived classes must to override inherited pure virtual functions or it will be abstract too.

Upvotes: 0

Kerrek SB
Kerrek SB

Reputation: 476990

No. A function is pure virtual if and only if it is declared with = 0.

Note that it is possible to provide a definition for a pure function, but you have to do that in two steps:

struct X
{
    virtual double f() = 0;  // pure, X is abstract
};

double X::f() { return 0; }  // definition

Usage:

X x;  // error, X is abstract

struct Y : X
{
    double f() override
    {
        return X::f();  // OK, calls pure virtual function
    }
};

Y y;  // OK, Y overrides X::f

Upvotes: 2

Related Questions