Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Const& parameter?

Status
Not open for further replies.

JediDan

Programmer
May 15, 2002
128
0
0
US
Hello,

I am using a class whose constructor takes a parameter as follows:
Code:
Class(const OtherClass& param1)

My code is basically:
Code:
OtherClass oc = new OtherClass();

Class c = new Class(oc);

...which generates errors. How do I properly pass the OtherClass instance to this constructor?
 
The constructor takes a const reference, not a pointer.
C++ != Java

Do this instead:
Code:
OtherClass oc;
Class c( oc );

or if you really want to use pointers for some reason, then do this:
Code:
OtherClass* oc = new OtherClass;
Class* c = new Class( *oc );
but then don't forget to delete those pointers when you don't need them anymore...
Code:
delete oc;
oc = NULL;
delete c;
c = NULL;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top