Search code examples
c++c++11templatesinheritanceassignment-operator

Copy assignment operator with multiple inheritance


My copy constructor below works fine, but I don't understand what is wrong with my copy assignment operator.

#include <iostream>

template <typename... Ts> class foo;

template <typename Last>
class foo<Last> {
    Last last;
public:
    foo (Last r) : last(r) { }
    foo() = default;
    foo (const foo& other) : last(other.last) { }

    foo& operator= (const foo& other) {
        last = other.last;
        return *this;
    }
};

template <typename First, typename... Rest>
class foo<First, Rest...> : public foo<Rest...> {
    First first;
public:
    foo (First f, Rest... rest) : foo<Rest...>(rest...), first(f) { }
    foo() = default;
    foo (const foo& other) : foo<Rest...>(other), first(other.first) { std::cout << "[Copy constructor called]\n"; }

    foo& operator= (const foo& other) {  // Copy assignment operator
        if (&other == this)
            return *this;
        first = other.first;
        return foo<Rest...>::operator= (other);
    }
};

int main() {
    const foo<int, char, bool> a(4, 'c', true);
    foo<int, char, bool> b = a;  // Copy constructor works fine.
    foo<int, char, bool> c;
//  c = a;  // Won't compile.
}

Error message:

error: invalid initialization of reference of type 'foo<int, char, bool>&' from expression of type 'foo<char, bool>'
         return foo<Rest...>::operator= (other);
                                              ^

Can someone point out the problem here?


Solution

  • Your return statement

    return foo<Rest...>::operator= (other);
    

    Returns a foo<Rest...> (that's the type of reference operator= is defined with). But it does so from an operator that is supposed to return a foo<First, Rest...>&.

    Essentially, you return a Base where a Derived& reference is expected. The reference simply won't bind.

    Fortunatly the fix is easy: don't return the result of foo<Rest...>::operator=, return *this instead.

    foo& operator= (const foo& other) {  // Copy assignment operator
        if (&other == this)
            return *this;
        first = other.first;
        foo<Rest...>::operator= (other);
        return *this;
    }