Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

BrianH

Question about copy constructors

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

Hello, say I have a base class, ClassA, which has a copy constructor defined for it. Now, I derive ClassB from ClassA and i also define a copy constructor for ClassB. My question is, does the copy constructor for the base ClassA get called automatically inside the copy constructor for ClassB? I know it happens for the regular constructor... thanks, Brian

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
not unless it''s virtual.

Share this post


Link to post
Share on other sites
virtual constructors? I don''t think so.

Both the original poster''s question and the AP''s misinformed answer could have been checked with a test program. Here''s an example.

  
#include <iostream>

using namespace std;

class Base
{
public:
Base () { cout << "Base ctor" << endl; }
Base (const Base &org) { cout << "Base copy ctor" << endl; }
};

class Derived : public Base
{
public:
Derived () { cout << "Derived ctor" << endl; }
Derived (const Derived &org) { cout << "Derived copy ctor" << endl; }
};

int main ()
{
Derived dorg;
Derived dcopy (dorg);
return 0;
}

Compile & run this program to get your answer.
Note: try to make the copy ctor virtual and it won''t compile.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!