Virtual keyword and functions

Started by
20 comments, last by rypyr 19 years, 9 months ago
Hey, I had a real simple question about the virtual keyword. Lets say I have the following base class and a derived class

class A {
    public :
         virtual void Create() { }
};

class B : public A {
    public :
        virtual void Create() { }
};

Now what I wanted to know was - if I do something like the following

A *a = new B;
a->Create();
Now it seems to call the right Create function. If I remove the virtual keyword it still calls the right function. So it is necessary to have the virtual keyword in the derived class ? Secondly what could be the drawbacks be of not having the virtual keyword in the derived class. Thank you.
The more applications I write, more I find out how less I know
Advertisement
The virtual keyword is not required for derived classes. I guess most programmers use it for either consistancy or as a reminder that the function is an implementation of a base class virtual function. It maked no difference to the code if you put virtual in the derived class or not.

Skizz
Like Skizz said, the virtual keyword is not required for derived classes. However, by leaving it in, you can further derive classes (in your example, you can create a class C derived from B with a public method called Create).
Hey,

S what you are saying is, If I do something like this

class A {    public :        virtual void Create();};class B : public A{    public :        void Create();};class C : public B{    public :        void Create();};


Now this will give an error. Right?

Thanks.
The more applications I write, more I find out how less I know
It won't giv you compile-time error but :

A *a = new C;A->Create();   // B::Create is called here, because B::Create isn't virtual and threrefore C::Create do not 'inherit' from it
www.tmreality.com
Quote:Original post by tomek_zielinski
It won't giv you compile-time error but :

A *a = new C;

A->Create(); // B::Create is called here, because B::Create isn't virtual and threrefore C::Create do not 'inherit' from it



Wrong.

class Base {public:  virtual void blah() { cout << "Base" << endl; }};class Child {public:  void blah() { cout << "Child" << endl; }};class Grandchild { public:  void blah() { cout << "Grandchild" << endl; }};


Try it:

Base* b = new Grandchild();
b->blah();

It should output "Grandchild".

Regards,
Jeff
Plus if you make a function pure virtual, by typing:
virtual void Create () = 0;

you've made the class into an interface and so it will give errors if you try to instantiate that class. But any classes derived from it containing no pure virtual functions can be instantiated. This also means that you have to supply an implementation of any pure virtual functions from its base class, even if your implementation is a no-op.

-Auron
Quote:Original post by Auron
Plus if you make a function pure virtual, by typing:
virtual void Create () = 0;

you've made the class into an interface and so it will give errors if you try to instantiate that class. But any classes derived from it containing no pure virtual functions can be instantiated. This also means that you have to supply an implementation of any pure virtual functions from its base class, even if your implementation is a no-op.

-Auron


Yes, but this is outside of the original question about virtual functions though.

Sometimes you want the base class to implement the function as a no-op so that you can instantiate an instance of a base class object, so it's not always the case that you want the function to be pure virtual.
Very true. I was more mentioning it as a case wherein the virtual keyword is necessary (at least if your design would predicate that the base class never be instantiated).

-Auron
Quote:

Quote:
Quote:Original post by tomek_zielinski
It won't giv you compile-time error but :

A *a = new C;

A->Create(); // B::Create is called here, because B::Create isn't virtual and threrefore C::Create do not 'inherit' from it



Wrong.

class Base {
public:
virtual void blah() { cout << "Base" << endl; }
};

class Child {
public:
void blah() { cout << "Child" << endl; }
};

class Grandchild {
public:
void blah() { cout << "Grandchild" << endl; }
};



Try it:

Base* b = new Grandchild();
b->blah();

It should output "Grandchild".

Regards,
Jeff



Sorry - my mistake. So I guess in such a case you cannot access Child::blah()??
www.tmreality.com

This topic is closed to new replies.

Advertisement