Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just 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.

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,397
    Several copy constructors? Please explain. There is only one true copy constructor signature: ClassName( const ClassName& );

    Normally, the declaration and implementation goes like this.
    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;
    }
    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.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Linux User twoHats's Avatar
    Join Date
    Jan 2005
    Location
    NH, USA
    Posts
    280
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •