Jump to content
  • Advertisement
Sign in to follow this  

[SOLVED] Copy Constructors (C++)

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

OK, I am using a prototype pattern for something, but I keep getting compiler errors when I try and implement the clone method.
virtual CEntity *clone()
{
  return new CEntity(*this);
}
"note: candidates are: CEntity::CEntity(const CEntity&)" It stops complaining when I make my own copy constructor, but there is a bunch of fields in which I would have to manually assign, and none of them require new memory to be allocated ... so it seems a default copy constructor should be the right way to do it. The CEntity class is a sub class of ISceneNode which is a sub class of IUnknown (Irrlicht Engine stuff), I have looked at both these classes and it doesn't seem that there is anything wierd going on that would prevent me from using a copy constructor ... I must be missing something I never learned? [Edited by - AsOne on April 9, 2006 1:05:00 PM]

Share this post


Link to post
Share on other sites
Advertisement
correct me if I'm wrong but AFAIK if you provide one constructor, then the language standard is to not provide defaults for anything else. So if you are making your own constructors at all, there will be no default copy constructor, and thus, you would have to write your own.

If it works when you write your own, then why not just do that?

Share this post


Link to post
Share on other sites
It is not clear what the problem is because you didn't post the relevant code and you didn't even post the errors. The code below compiles. Try to figure out how it is different from yours.

class foo
{
virtual foo * clone()
{
return new foo(*this);
}

int f;
};

class bar : public foo
{
virtual foo * clone()
{
return new bar(*this);
}
int b;
};

void main()
{
bar b;

foo * c = b.clone();
foo * x = &b;
foo * y = x->clone();
}


Share this post


Link to post
Share on other sites
Thats the problem, I have no idea why it doesn't compile.
Quote:
Original post by Morpheus011
correct me if I'm wrong but AFAIK if you provide one constructor, then the language standard is to not provide defaults for anything else. So if you are making your own constructors at all, there will be no default copy constructor, and thus, you would have to write your own.

This worked.

class CCar
{
public:
CCar(int wheels, int seats) : Wheels(wheels), Seats(seats)
{

}
virtual CCar *clone()
{
return new CCar(*this);
}
virtual void display()
{
cout << "Wheels: " << Wheels << endl;
cout << "Seats: " << Seats << endl;
}
private:
int Wheels;
int Seats;
};

class CSportsCar : CCar
{
public:
CSportsCar(int wheels, int seats) : CCar(wheels, seats)
{

}
virtual CCar *clone()
{
return new CSportsCar(*this);
}
};

int main()
{
CCar *car = 0;

CCar red(4, 6);
CSportsCar blue(2, 2);

car = red.clone();
car->display();
delete car;

car = blue.clone();
car->display();
delete car;

return 0;
}




I get this error (in first post):

"note: candidates are: CEntity::CEntity(const CEntity&)"
CEntity::CEntity(const irr::core::stringw&, irr::scene::IAnimatedMesh*, irr::IrrlichtDevice*)

... and the first part points to this line:
"return new CEntity(*this);"
... the second part points to the end of the constructor.

This is all the relevant code I can think of ...

CEntity::CEntity(
const stringw &name,
IAnimatedMesh *mesh,
IrrlichtDevice *device
) : ISceneNode(0, device->getSceneManager())



Could there be something else affecting this? (super class?) I'm not sure what other code I could post.

EDIT: I thought maybe that if the class has pointers or something, it won't let you use the default copy constructor for safety purposes. But this compiled fine.

class Base
{
public:
Base(int *p) : P(p)
{
}
Base *clone()
{
return new Base(*this);
}
int *P;
};

int main()
{
Base base1(new int);
*base1.P = 42;
Base *base2 = base1.clone();
cout << *base1.P << endl;
cout << *base2->P << endl;
delete base1.P;
delete base2;
return 0;
}

Share this post


Link to post
Share on other sites
This is the only thing I can think of right now but maybe a parent of CEntity is hiding the copy constructor and that is why your getting errors and when you write your own, you are overriding the hidden one...

Share this post


Link to post
Share on other sites
Quote:
Original post by Morpheus011
correct me if I'm wrong but AFAIK if you provide one constructor, then the language standard is to not provide defaults for anything else.


The default constructor will not be generated. The copy constructor will.

Share this post


Link to post
Share on other sites
Quote:
Original post by Adam Hamilton
This is the only thing I can think of right now but maybe a parent of CEntity is hiding the copy constructor and that is why your getting errors and when you write your own, you are overriding the hidden one...

ISceneNode only has one constructor, as well as IUnknown.

Share this post


Link to post
Share on other sites
const correctness is certainly a good idea here, but it wont help with the compilation problem, in this case.

Share this post


Link to post
Share on other sites
CEntity::CEntity(const CEntity&) < I would assume that you dont have const when you defined it? As far as your other code, What line is bitching?

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!