Jump to content
  • Advertisement
Sign in to follow this  
FirstDouchaEver

Copy Constructors

This topic is 4526 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'm trying to make a copy constructor thus: cTree(void); ~cTree(void); cTree(cTree treeclone); in my cTree class. the contents of the new class will be set according to those in treeclone. I get the following error upon compilation: C2652: 'cTree' : illegal copy constructor: first parameter must not be a 'cTree' I don't get it? Can anyone help - is this the wrong way to declare the constructor?

Share this post


Link to post
Share on other sites
Advertisement
Should be: cTree(const cTree &other);

(Although the const is not strictly required, the fact that the parameter must be passed by reference is; otherwise, the copy constructor would need to be invoked to pass the parameter into the copy constructor, which obviously isn't going to work).

Share this post


Link to post
Share on other sites
Quote:
Original post by FirstDouchaEver
Great that compiles now, thanks - so I must pass in the tree to be cloned as a pointer?

No, you pass it as a reference, not pointer.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
http://www.parashift.com/c++-faq-lite/references.html

Share this post


Link to post
Share on other sites
jpetri is right. But not just in copy constructors, in most cases, you should pass class parameters as const references or const pointers for performance reasons.

Share this post


Link to post
Share on other sites
Thanks everyone I shall read up on referencing etc. and the const parameters.

It seems to work fine now - now I have the problem of rendering as I'm trying to render more than one copy of the same mesh - but that's in a different post.

Thanks all

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!