Sign in to follow this  
FirstDouchaEver

Copy Constructors

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
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

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