Jump to content
  • Advertisement
Sign in to follow this  
eben

Difference between "virtual" and "pure virtual" functions

This topic is 4860 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

What is the difference between the "virtual function": virtual int doStuff(); ...and the "pure virtual function": virtual int doStuff() = 0;

Share this post


Link to post
Share on other sites
Advertisement
A pure virtual functions doesn't have a body. You cannot create classes with pure virtual functions in them.

// You cannot create a 'pure' GraphObject
class IGraphObject
{
public:
virtual void Draw() = 0;
};

// But you can create derived classes
class CTriangle : public IGraphObject
{
public:
virtual void Draw();
};
class CLine : public IGraphObject
{
public:
virtual void Draw();
};

What you can do, is to hold a pointer to a GraphObject.

IGraphObject * object = new CTriangle;
object->Draw();

Share this post


Link to post
Share on other sites
When a function is virtual it can have a function body. If you want to overload that function you can, but you don't have to, which is probably what you want in most cases of inheritance.

Pure virtual functions have no body and MUST be overloaded. You cannot create an instance of a class with a pure virtual function, so something else has to inherit from it and overload all pure virtual functions. This is generally what is done for Interface Inheritance, where the interface class is just a bunch of pure virtual functions and all the actual code is in classes that inherit it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Konfusius
I guess you mean "overriden" when you speak of "overloaded" ;)

Actually, even though they mean the same thing, I'm pretty sure what I said is correct too. Google seems to agree with me.

Share this post


Link to post
Share on other sites
Pretty much what everyone already said, only unlike what they have said, pure virtual functions can have a body, they just don't have to have a body. The most common use for this is pure virtual destructors.

Share this post


Link to post
Share on other sites
Thanks for the quick reply. Also can you use casting to use a function of a super class from a base class like in java?

class BaseClass
-functionA()

|
|
V

class SubClass : public BaseClass
-functionA()

Based on the above, would you be able to call functionA() in the sub-class class in such a way that it would actually call functionA() from the base class?

Share this post


Link to post
Share on other sites
Quote:
Original post by eben
Thanks for the quick reply. Also can you use casting to use a function of a super class from a base class like in java?

class BaseClass
-functionA()

|
|
V

class SubClass : public BaseClass
-functionA()

Based on the above, would you be able to call functionA() in the sub-class class in such a way that it would actually call functionA() from the base class?


Yes. Instead of calling the function as just functionA(), call it as BaseClass::functionA().

Share this post


Link to post
Share on other sites
Quote:
Original post by Polymorphic OOP
Pretty much what everyone already said, only unlike what they have said, pure virtual functions can have a body, they just don't have to have a body. The most common use for this is pure virtual destructors.


It should be noted that even if a pure virtual function has a body, that still won't allow you to instantiate the class.

Quote:
Original post by intrest86
Quote:
Original post by Konfusius
I guess you mean "overriden" when you speak of "overloaded" ;)

Actually, even though they mean the same thing, I'm pretty sure what I said is correct too. Google seems to agree with me.


You mean overriding. Overloading is the process of providing multiple functions of the same name but with different signatures.

Share this post


Link to post
Share on other sites
Quote:
Original post by intrest86
Quote:
Original post by Konfusius
I guess you mean "overriden" when you speak of "overloaded" ;)

Actually, even though they mean the same thing, I'm pretty sure what I said is correct too. Google seems to agree with me.


"Overriding" and "overloading" are two completely different things. "Overriding" a function is feature of inheritance in which a function in the base class is replaced by a function in the derived class. "Overloading" a function refers to providing multiple versions of the function that differ by their parameters. Note that overloaded functions can also be overridden.

One small exception is "overloading" an operator, which can also refer to replacing a default operator. I think it might be more proper to refer to replacing a default operator as "overriding" it, but that's not the generally accepted term.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!