Copy Constructors

Started by
5 comments, last by FirstDouchaEver 17 years, 11 months ago
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?
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).
Great that compiles now, thanks - so I must pass in the tree to be cloned as a pointer?

Cheers for your help and quick response
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.
http://www.parashift.com/c++-faq-lite/references.html
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.
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

This topic is closed to new replies.

Advertisement