Jump to content
  • Advertisement
Sign in to follow this  
y2jsave

virtual functions doubt !!

This topic is 2902 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,
I have read somewhere that when we call virtual functions from constructors, its always the function of the current class which gets called .
but when i wrote a program to test it , it gave me results which contradict with what i have read ..

please tell where iam wrong ??


#include<iostream>
using namespace std;


class base
{
public:
virtual void func()
{
cout<<"in base class func";
}
};

class derived2 : public base
{
public:

void func()
{
cout<<"\n in derived2 class func";
}

};

class derived : public base
{
public:

void func()
{
cout<<"\n in derived class func";
}

derived()
{
base* b ;
b = new derived2;
b->func();

base* b1 = new base;
b1->func();

base* b2 = this;
b2->func();

}
};

int main()
{
derived d ;


return 0;
}





output::


in derived2 class func
in base class func
in derived class func



Thanks in advance
y2jsave

Share this post


Link to post
Share on other sites
Advertisement
the first thing you're doing wrong is not using source tags.

And apparently, virtual functions are set with the new keyword as the example here shows. When the new function allocates memory, it treats the new class as if it was derived instead of base.

I'm not sure how virtual functions works as I've never found the need for them.

Share this post


Link to post
Share on other sites
That is correct that virtual functions called from constructors call the function of that class (as opposed to how normal virtual functions will call the derived class's function). However, this only applies to the object being constructed, and not any sub objects that happened to be created in the constructor.

#include <iostream>

using namespace std;

class Base
{
public:
Base()
{
cout << "base constructor" << endl;
notVirtual();
isVirtual();
}

void notVirtual()
{
cout << "base not virtual" << endl;
}

virtual void isVirtual()
{
cout << "base virtual" << endl;
}
};

class Derived:public Base
{
public:
Derived():Base()
{
cout << "Derived constructor" << endl;
}

void notVirtual()
{
cout << "derived not virtual" << endl;
}

// not necessary to declare this as virtual unless you are expecting to have other classes inherit from Derived
virtual void isVirtual()
{
cout << "derived virtual" << endl;
}
};

int main(void)
{
Base *base = new Derived();
return 0;
}




with the expected output as:

Quote:
base constructor
base not virtual
base virtual
Derived constructor

Share this post


Link to post
Share on other sites
Your example doesn't accurately represent what you are trying to demonstrate. If you had just called the virtual func function within the derived class constructor you would have observed the expected behavior, as noted above.

Since constructors run form the base object down the hierarchy, calling a virtual function from within a constructor can only ever call the local version in the same type - the derived instances haven't been created yet (and no VTABLE has been created for those derived instances).

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!