开发者

What is the difference between a pure virtual and a virtual

I am modifying code that the grand-parent class is a pure virtual which include a pure virtual version of function XYZ ; the then parent class declares XYS as virtual and it has an implementaion for it. 开发者_C百科 Then the child class declares XYZ as a regular function with a different implementation from that of the parent1 9which in itself is confusing to me). When I call the function XYZ from another object, which implementation gets executed? the parent one or the child one? Thanks


When I call the function XYZ from another object, which implementation gets executed? the parent one or the child one?

Let's find out:

struct A {
  virtual void f() = 0;
};

struct B : A {
  virtual void f() { cout << "B::f\n"; }
};

struct C : B {
  virtual void f() { cout << "C::f\n"; }
};

int main() {
  C().f();
  return 0;
}


Pure virtual functions don't need a definition (as opposed to virtual functions), and they make their class abstract. Abstract classes can't have an object created from, except when they act as base class object.

Your confusion seems to center around the presence or absence of the virtual keyword. If a function is declared virtual in a base class, then a function of a derived class, whether or not you put a virtual keyword, will automatically become virtual if it has the same name, parameter types and constness.

So if you call XYZ on a grandparent* or parent* that actually points to a child object, then the XYZ of the child object will be executed.


A pure virtual function just means that no class with a pure virtual function can be instantiated. Once the parent class overrides it, it just becomes a normal virtual function. Once a function has been declared virtual in a base class, it is always virtual, regardless of whether or not the inheriting class defines it as virtual.

Edit: That means that calling it is just calling a virtual function like any other- that is, the most derived class's implementation will be called.


A pure virtual function usually has no implementation and creates the "abstractness" of the class.

The compiler will not let you create an instance of a class that is abstract. Any class that derives from this class remains abstract unless all the pure virtual functions it inherits have been implemented (and it doesn't add any new ones). Such a class is called concrete.

Note that a pure virtual function may be given an implementation (although it cannot be inlined for syntax reasons). In addition, you can have a pure virtual destructor, and then it must be given an implementation (even if it is an empty one).

You indicate a pure virtual function by adding =0 at the end thus:

virtual void foo(); // not pure
virtual void bar() = 0; // pure virtual


class sample
{
public:
   virtual void fun(); //virtual function
   virtual void sun()=0; //pure virtual function
};

Pure virtual function is declared by assigning 0, as done above. Providing definition for pure virtual functions is optional, while providing definition for virtual functions is compulsory, otherwise it will not compile. Also, you cannot create instance of a class that defines even a single pure virtual function.


A virtual function is one that can be overridden in a derived class.

A pure virtual function is one that has no implementation at all and therefore MUST be overridden in a derived class.

You cannot create an instance of a class with a pure virtual function, or a class that derives from it unless you override any pure virtual functions with your own implementation.


In the case of a virtual function, if you call xyz on an object of a child class, always the child version of xyz (Child::xyz()) will be executed.

It would not be a case with non-virtual overridden methods. Then if you had a pointer Parent* ptr = &child, ptr>xyz() would actually execute Parent::xyz(). But in your case it is Child::xyz(). That is why you use the virtual keyword.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜