C++ : copy-and-swap idiom, alternative constructor

1.1k views Asked by At

NB: This question follows a previous one, I hope it is okay to still ask it as a new question.

I am trying to implement the "three and a half big rule" (copy-and-swap idiom) for a tree class, which looks like this:

class Tree
{
    friend void swap(Tree &first, Tree &second); // Swap function

public:
    Tree(const double &a, const double &b, int depth); // Public constructor (derived from the default (private) constructor)
    Tree(const Tree &other); // Copy constructor
    ~Tree(); // Destructor
    Tree & operator=(Tree other); // Copy-assignement operator


private:        
    Tree(double *a, double *b, int depth, int maxDepth); // Default (private) constructor

    double *a, *b;
    int depth, maxDepth;    
    Tree *leftChild, *rightChild;
};

I have been trying to follow this guideline. Here is what my copy-assignment operator looks like:

Tree & Tree::operator=(Tree other)
{
    swap(*this, other);
    return *this;
}

I am having a hard time getting my public constructor to work. Someone suggested I do something like:

Tree::Tree(const double &a, const double &b, int depth)
{
    double aTemp(a), bTemp(b);
    swap(*this, Tree(&aTemp, &bTemp, depth, depth));
}

I am not sure this idea works. In any case I get the following error from the compiler:

invalid initialization of non-const reference of type 'Tree&' from an rvalue of type 'Tree'
in passing argument 2 of 'void swap(Tree&, Tree&)'

I tried the following idea instead, which I thought would work:

Tree::Tree(const double &a, const double &b, int depth)
{
    double aTemp(a), bTemp(b);
    *this = Tree(&aTemp, &bTemp, depth, depth);
}

But it does not seem to be working either. I think the problem is that when I call the copy-assignment operator (*this = Tree(&aTemp, &bTemp, depth, depth)), the copy constructor should be called (since the argument of the copy-assignement operator is passed by value), but it seems that this is not happening. I do not understand why.

Thanks in advance for helping!

1

There are 1 answers

2
jamesdlin On BEST ANSWER

invalid initialization of non-const reference of type 'Tree&' from an rvalue of type 'Tree' in passing argument 2 of 'void swap(Tree&, Tree&)'

C++ does not allow passing anonymous objects by non-const reference. The intent is to prevent callers from accidentally throwing away the results of functions that write to a reference argument.

You instead could do:

Tree::Tree(const double &a, const double &b, int depth)
{
    double aTemp(a), bTemp(b);
    Tree temp(&aTemp, &bTemp, depth, depth);
    swap(*this, temp);
}

But it does not seem to be working either. I think the problem is that when I call the copy-assignment operator (*this = Tree(&aTemp, &bTemp, depth, depth)), the copy constructor should be called (since the argument of the copy-assignement operator is passed by value), but it seems that this is not happening. I do not understand why.

How are you determining that it's not working? The compiler might elide away the copy to avoid doing unnecessary work. (That is why your copy-assignment operator takes the argument by value.)

BTW, if your compiler supports C++11, you could use delegating constructors instead.