-1

I've got this code here:

class DerivedClass : public BaseClass {
    SomeClass* a1;
    Someclass* a2;
public:
    //constructors go here
    ~DerivedClass() { delete a1; delete a2;}
    // other functions go here ....
};

My first question is as follows:

  • Can I write an "operator=" to "DerivedClass" ? (if your answer is yes, could you show me how?)

My second question is:

  • If the answer to the above is yes, could you show me how to make a "copy consructor" using the "operator=" that you wrote beforehand (if that is even possible)?
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 5
    Yes you can, and yes it is possible, but if we post the code for you you won't learn anything. Try yourself and post any problems you might have. – zennehoy Jul 01 '13 at 15:30
  • http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29 – DGomez Jul 01 '13 at 15:35
  • The usual pattern is to use the copy constructor in the assignment operator (copy and swap), not the other way around. Using the assignment operator in the copy constructor requires constructing the member variables before you can use the assignment operator, which is sub-efficient. – syam Jul 01 '13 at 15:35
  • The reason i'm posting here is because I don't know how to implement it, so it would be very kind of you to show me how. – user2266935 Jul 01 '13 at 15:43
  • Looks like an interview question... – Kerrek SB Jul 01 '13 at 19:38

2 Answers2

1

The body of copy ctor and op= depend on the way you plan to store your resources: a1 and a2 variables. If you need them to be copied to another class - you should write some function that will make a full copy of your SomeClass object. Another case - you can simply copy pointer value - but then be very careful about the way you use them and especially delete them. The easiest solution to the sharing resources problem would be using some smart pointer, for example boost::shared_ptr or c++11 std::shared_ptr.

So if you plan to copy your resources:

class DerivedClass : public BaseClass {
    SomeClass* a1;
    Someclass* a2;
public:

    // note: do not forget to assign zeroes to your pointers, unless  you use some kind of smart pointers
    DerivedClass()
    :a1(0), a2(0){}

    DerivedClass(const DerivedClass& d)
                :a1(0), a2(0)
    {
        *this = d;
    }

    DerivedClass& operator=(const DerivedClass& d)
    {
        if (this == &d)
            return *this;

        delete a1;
        a1 = d.a1->clone();

        delete a2;
        a2 = d.a2->clone();

        return *this;
    }


    //constructors go here
    ~DerivedClass() { delete a1; delete a2;}
    // other functions go here ....
};

You will also need SomeClass's clone() function, which going to copy your objects: class SomeClass { public:

    SomeClass* clone() const
    {
        return new SomeClass();
    }
};
Bogolt
  • 520
  • 1
  • 3
  • 9
1

Here's the best way to write the copy constructor and assignment operator, respecting the "Rule of Zero":

#include <optional>
class DerivedClass : public BaseClass
{
    std::optional<SomeClass> a1;
    std::optional<SomeClass> a2;
public:
    //constructors go here
};

The compiler will write correct code for the destructor, copy constructor, and copy assignment operators. If SomeClass is movable, you'll get move assignment and move constructor for free as well.

If SomeClass is polymorphic, you will need a clone() function like BogoIt mentioned. But even in that case it's valuable to use a smart pointer (std::unique_ptr would be appropriate).

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Which standard defines header ``? It doesn't appear to be the ISO/IEC 14882:2011 (the C++11 standard). Tables 14 and 15 (p435, and reproduced in [List of standard header files in C and C++](http://stackoverflow.com/questions/2027991/)) do not list `` as a header. – Jonathan Leffler Jul 01 '13 at 17:06
  • @Jonathan: oops, [it's only in namespace `std` starting with the C++14 draft](http://en.cppreference.com/w/cpp/utility/optional). Do you know a good C++11 alternative that's like `std::unique_ptr` but is copyable by copying the target object? – Ben Voigt Jul 01 '13 at 20:57
  • Succinctly, no — I haven't looked hard. Maybe Boost has a good one? (Version 1.54 was released today, 2013-07-01, to judge by my incoming email.) – Jonathan Leffler Jul 01 '13 at 21:04
  • @JonathanLeffler: If you're willing to pull in Boost, then `boost::optional` would be a candidate. – Ben Voigt Jul 01 '13 at 21:05