Sign in to follow this  

What is your idea about virtual functions?

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

The meaning of virtual depends on the context in which you use it. In computer programming, there are at least two meanings that I know of:

  • A virtual member function (of a class) is an object field which is bound at runtime with a message(instead of compile-time). This results in single polymorphic dispatch for that message. Or, if you prefer, if you mark a function as virtual, then the code that will be called will depend on the type of the object as it was created, instead of the locally known type. For example, in C++, ((Base*)(new Derived( )))->Func( ) calls Base::Func in the general case, but Derived::Func if the function is virtual.

  • A virtual member function (of a class) is a member function which has received no implementation. In particular, this prevents the class from being instantiated, and any classes inheriting the former must provide an implementation for the virtual function before they can be instantiated.



There might be other meanings though.

Share this post


Link to post
Share on other sites
A pure virtual function is defined by assigning a 0 to it's prototype in the class declaration. That renders the class an abstract class which means that you can't instantiate an instance of that class. So it basically just means that it is an interface and must be inherited from to use it.

Assuming you know what a virtual function is, that's the difference.

Share this post


Link to post
Share on other sites
As explained before, a virtual function is a function that is dynamically bound. A pure virtual function is a function that is dynamically bound and has no default implementation (it can have an implementation but can only be called explicitly). A class containing one or more pure virtual functions is an abstract class and cannot be instantiated:
#include <iostream>

class NormalClass
{

public:

void normalFunction()
{
std::cout << "NormalClass::normalFunction()\n";
}

};

class BaseClassWithVirtualFunction
{

public:

virtual void virtualFunction()
{
std::cout << "BaseClassWithVirtualFunction::virtualFunction()\n";
}

};

class BaseClassWithPureVirtualFunction
{

public:

virtual void virtualFunction() = 0;

};

class BaseClassWithPureVirtualFunctionWithImplementation
{

public:

virtual void virtualFunction() = 0;

};

void BaseClassWithPureVirtualFunctionWithImplementation::virtualFunction()
{
std::cout << "BaseClassWithPureVirtualFunctionWithImplementation::virtualFunction()\n";
}

class DerivedFromBaseClassWithVirtualFunction
:
public BaseClassWithVirtualFunction
{

public:

virtual void virtualFunction()
{
std::cout << "DerivedFromBaseClassWithVirtualFunction::virtualFunction()\n";
}

};

class DerivedFromBaseClassWithPureVirtualFunction
:
public BaseClassWithPureVirtualFunction
{

public:

virtual void virtualFunction()
{
std::cout << "DerivedFromBaseClassWithPureVirtualFunction::virtualFunction()\n";
}

};

class DerivedFromBaseClassWithPureVirtualFunction2
:
public BaseClassWithPureVirtualFunction
{

public:

virtual void virtualFunction()
{
std::cout << "DerivedFromBaseClassWithPureVirtualFunction2::virtualFunction()\n";
}

};

class DerivedFromBaseClassWithPureVirtualFunctionWithImplementation
:
public BaseClassWithPureVirtualFunctionWithImplementation
{

public:

virtual void virtualFunction()
{
std::cout << "DerivedFromBaseClassWithPureVirtualFunctionWithImplementation::virtualFunction()\n";
}

void callPureVirtualFunction()
{
// explicit call to pure virtual function with implementation
BaseClassWithPureVirtualFunctionWithImplementation::virtualFunction();
}

};

int main()
{
NormalClass nc;
BaseClassWithVirtualFunction bcwvf;
// BaseClassWithPureVirtualFunction bcwpvf - not instantiable
// BaseClassWithPureVirtualFunctionWithImplementation bcwpvfwi - not instantiable
DerivedFromBaseClassWithVirtualFunction dfbcwvf;
DerivedFromBaseClassWithPureVirtualFunction dfbcwpvf;
DerivedFromBaseClassWithPureVirtualFunction2 dfbcwpvf2;
DerivedFromBaseClassWithPureVirtualFunctionWithImplementation dfbcwpvfwi;

// call through objects - static binding
nc.normalFunction(); // calls NormalClass::normalFunction
bcwvf.virtualFunction(); // calls BaseClassWithVirtualFunction::virtualFunction
dfbcwvf.virtualFunction(); // calls DerivedFromBaseClassWithVirtualFunction::virtualFunction
dfbcwpvf.virtualFunction(); // calls DerivedFromBaseClassWithPureVirtualFunction::virtualFunction
dfbcwpvf2.virtualFunction(); // calls DerivedFromBaseClassWithPureVirtualFunction2::virtualFunction
dfbcwpvfwi.virtualFunction(); // calls DerivedFromBaseClassWithPureVirtualFunctionWithImplementation::virtualFunction

// demonstrate that a pure virtual function can be called
dfbcwpvfwi.callPureVirtualFunction(); // calls DerivedFromBaseClassWithPureVirtualFunctionWithImplementation::callPureVirtualFunction(),
// which calls BaseClassWithPureVirtualFunctionWithImplementation::virtualFunction

// pointers allow dynamic binding (as do references)
// pbcwvf has static type "BaseClassWithVirtualFunction *" and dynamic type "DerivedFromBaseClassWithVirtualFunction *"
BaseClassWithVirtualFunction * pbcwvf = &dfbcwvf;
// pbcwpvf has static type "BaseClassWithPureVirtualFunction *" and dynamic type "DerivedFromBaseClassWithPureVirtualFunction *"
BaseClassWithPureVirtualFunction * pbcwpvf = &dfbcwpvf;
// pbcwpvfwi has static type "BaseClassWithPureVirtualFunctionWithImplementation *" and dynamic type "DerivedFromBaseClassWithPureVirtualFunctionWithImplementation *"
BaseClassWithPureVirtualFunctionWithImplementation * pbcwpvfwi = &dfbcwpvfwi;

// call through pointers - dynamic binding
pbcwvf->virtualFunction(); // calls DerivedFromBaseClassWithVirtualFunction::virtualFunction
pbcwpvf->virtualFunction(); // calls DerivedFromBaseClassWithPureVirtualFunction::virtualFunction
pbcwpvfwi->virtualFunction(); // calls DerivedFromBaseClassWithPureVirtualFunctionWithImplementation::virtualFunction

// change dynamic types of pointers
// pbcwvf has static type "BaseClassWithVirtualFunction *" and dynamic type "BaseClassWithVirtualFunction *"
pbcwvf = &bcwvf;
// pbcwpvf has static type "BaseClassWithPureVirtualFunction *" and dynamic type "DerivedFromBaseClassWithPureVirtualFunction2 *"
pbcwpvf = &dfbcwpvf2;

// call through pointers - dynamic binding
pbcwvf->virtualFunction(); // calls BaseClassWithVirtualFunction::virtualFunction
pbcwpvf->virtualFunction(); // calls DerivedFromBaseClassWithPureVirtualFunction2::virtualFunction
}


Enigma

Share this post


Link to post
Share on other sites

This topic is 4383 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this