Copy Constructors
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?
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).
(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
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement