Jump to content
  • Advertisement
Sign in to follow this  
CRACK123

Virtual keyword and functions

This topic is 5097 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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()??

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!