Archived

This topic is now archived and is closed to further replies.

vesoljc

copy con & assign oper. in derived objects

Recommended Posts

vesoljc    108
if my base class has custom copy constructor and custom assignment operator and my derived class also, do i have to manually call the base class const and operator? coz, i tried this: my base class serves as an ID class, it has an ID string and few functions for ID access. allthough compiler generated copy constructor and assignemnt oper would work, i intetionaly added them for debugging and learning i then create a new class, derive from ID class and again add copy con and assign oper... i then create two classes, give them diffrent id and try to assign one to another. new class data gets transfered ok, but id''s dont change... so i''m guessing that i have to call them manually?

Share this post


Link to post
Share on other sites
vesoljc    108
well, i found out that copy constructors work ok, but assignment operators dont, unless i do this:


const derived_class& derived_class::operator=(const derived_class& ldRhs)
{
base_class::operator=(ldRhs);
// transfer data from this class

return(*this);
}


is this ok?

[edited by - vesoljc on January 25, 2004 7:57:45 AM]

Share this post


Link to post
Share on other sites
Jingo    582
Thats fine. You would call the copy constructor of the base class using an initialization list


derived_class::derived_class(const derived_class& der)
:base_class(der)
{}

Share this post


Link to post
Share on other sites