Results 1 to 3 of 3
In one of my interview, interviewer asked the following question on deep copy constructor:
When we are externally overloading the copy constructor is there any optimization strategies when we are ...
- 08-20-2010 #1Just Joined!
- Join Date
- May 2009
- Posts
- 21
Optimizing of copy constructor in C++
In one of my interview, interviewer asked the following question on deep copy constructor:
When we are externally overloading the copy constructor is there any optimization strategies when we are using several copy constructors.
- 08-21-2010 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Several copy constructors? Please explain. There is only one true copy constructor signature: ClassName( const ClassName& );
Normally, the declaration and implementation goes like this.
If you follow this pattern in all of your non-trivial classes, then you gain a lot of stability and control. The virtual destructor means that you can call delete on a pointer to any base class or derived class and the correct destructor will be called, which is a common error a lot of new C++ programmers make. In any case, you definitely want to make sure that all member variables that do not have default constructors defined are initialized to a sane value in the default constructor, and are initialized with the same member in the copied object. There are times when this is not possible, such as when one member variable is effectively a singleton, in which case you will have to initialize it appropriately as needed.Code:class BaseClass; class NewClass : public BaseClass { private: MemberTypeA memberA; MemberTypeB memberB; public: NewClass(); // default consturctor NewClass( const NewClass& cpy ); // copy constructor virtual ~NewClass(); // virtual destructor NewClass& operator=( const NewClass& ); // assignment operator }; NewClass::NewClass() // Default constructor implementation : memberA(MemberTypeADefaultValue), // Member initialization memberB(MemberTypeBDefaultValue) { // Body of constructor } NewClass::NewClass( const NewClass& cpy ) // Copy ctor implementation : BaseClass(cpy), // Copy base class first memberA(cpy.memberA), // Now, copy member variables memberB(cpy.memberB) { // Body of the constructor } NewClass::~NewClass() { // Do anything special here in body of destructor // such as freeing allocated memory, like pointer members. } NewClass& NewClass::operator=( const NewClass& rhs ) { // First, assign BaseClass part of this from rhs. // You can use a simple cast here unless there are multiple base // base classes or if there are virtual base classes, in which case // you will need to use a dynamic_cast. *(BaseClass*)this = rhs; /* Example using dynamic_cast BaseClass& basepart = dynamic_cast<BaseClass&>(*this); basepart = rhs; */ // Now, assign member variables memberA = rhs.memberA; memberB = rhs.memberB; // Finally, return a reference to this as this has to return an LHS type return *this; }Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 08-22-2010 #3
What Rubberman said! - well put and added value with virtual destructor info -
Interview may have been a trick question?- Clouds don't crash - Bertrand Meyer
registered Linux user 393557
finally - hw to brag about - but next year it will look pitifully quaint:
Athlon64 X2 3800 - 1G PC3200 - 250G SATA - ati radeon x300
circa 2006


Reply With Quote