Sign in to follow this  

Virtual Function Question

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

Hi guys, Does it matter if I don't use the 'virtual' keyword in the derived class ? It works both ways but I'm not sure what's going on inside. Maybe it depends on if it's pure ? Here is some code: class CBaseClass { public: virtual void DoSomething()=0; }; class CDerivedClass : public CBaseClass { virtual void DoSomething(); // like that ? void DoSomething(); // or like this ? } Also, adding the '=0' makes it pure right ? What's the difference between pure and non-pure ? Thanks. I'm using VC++ 6.0 (SP5) Take care, Cyrus

Share this post


Link to post
Share on other sites
If you declare a function virtual in a base class, overrides of that function implicitly declared virtual in all derived classes. (However, it's bad form to leave off the virtual in the derived classes.)

A pure virtual function must be overridden in derived classes in order for the derived classes to be instantiated. A class with any non-overriden pure virtual functions is called an abstract class.

Share this post


Link to post
Share on other sites
Regarding your first question: there is no difference. The method will be implicitly virtual, whether you add the keyword or not.

WRT your second question: Yes, the '= 0' makes it a pure virtual function and the difference is that a pure virtual function makes the class abstract, that is it cannot be instanciated. Any derived classes that do not provide an implementation for all pure virtual methods of the base class will also be abstract classes. You can use this behaviour to force the use of derived classes.

Your third question: upgrade to VC.NET! NOW! Just kidding [smile]

[edit]
Too slow... [sad]
[/edit]

Share this post


Link to post
Share on other sites
Quote:
Original post by mike25025
the derived class doesnt need the virtual keyword


Unless you plan to have classes derived from it able to override the DoSomething function as well. Aka:

struct A //struct A == class A { public:
{
virtual void DoSomething() = 0; //MUST be implemented in derived class. This class CAN NOT be created.
};

struct B : public A
{
virtual void DoSomething(); //MAY be implemented in derived class. This class CAN be created.
};

struct C1 : public B
{
void DoSomething(); //SHOULD NOT be implemented in derived class, most likely... reason is explained bellow. CAN be created.
};

struct C2 : public A
{
void DoSomething(); //Same as C1
};

struct D : public C1
{
void DoSomething(); //This is were things get interesting...
};

//Now, the difference, illustrated in a function:
void function( void )
{
D my_d;
my_d.DoSomething(); //Calls D::DoSomething() as expected.

A * my_ptr = new D;
my_ptr->DoSomething(); //Calls C1::DoSomething(), probably not expected.
delete my_ptr;

my_ptr = new B;
my_ptr->DoSomething(); //Calls B::DoSomething() as expected.
delete my_ptr;
}


Quote:
pure meens the the function doesnt exsist and needs to be created in a derived class


It is complete valid in C++ to do the following:

class CBaseClass
{
public:
virtual void DoSomething() = 0;
};

void CBaseClass::DoSomething()
{
//...
}


The class will not be createable and derived classes will need to implement DoSomething(). However, this allows the following code:

class CDerviedClass : public CBaseClass
{
public:
virtual void DoSomething();
}

void CDerivedClass::DoSomething( void )
{
CBaseClass::DoSomething(); //*
//do more stuff...
}


Quote:
classes with pure virtual functions cant be created


edit: unbrokened forum.

* We're "adding onto", not "replacing" DoSomething() in this case. In reality we're replacing DoSomething, but we're calling the base class's DoSomething() first, so it has the same effect.

Share this post


Link to post
Share on other sites
Quote:
Original post by MaulingMonkey
Quote:
Original post by mike25025
the derived class doesnt need the virtual keyword


Unless you plan to have classes derived from it able to override the DoSomething function as well.

No, once virtual, always virtual. You will get exactly the same effect if you write virtual in the child or intermediate base as long as the original declaration was virtual. It's entirely optional to put virtual in a child once the function is declared virtual in a base, and it never changes the functionality of the code if you do or do not continue writing virtual in each override.

Share this post


Link to post
Share on other sites

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