具有多重继承的复制赋值运算符

Copy assignment operator with multiple inheritance

我下面的复制构造函数工作正常,但我不明白我的复制赋值运算符有什么问题。

#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: invalid initialization of reference of type 'foo<int, char, bool>&' from expression of type 'foo<char, bool>'
         return foo<Rest...>::operator= (other);
                                              ^

有人能指出这里的问题吗?

你的return声明

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

Returns a foo<Rest...> (这是 operator= 定义的引用类型)。但它是由一个应该 return a foo<First, Rest...>&.

的操作员执行的

本质上,您 return 一个 Base,其中需要一个 Derived& 引用。引用根本不会绑定。

幸运的是,修复很简单:不要 return foo<Rest...>::operator=、return *this 的结果。

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

看起来 return 来自 operator= 的派生 class 是不正确的:

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

它 return 的基数 class,而它应该是 *this。改成

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

感谢StoryTeller,这是一个优化的完全编译解决方案(operator=委托给另一个命名成员名称copy_data,不检查自赋值,递归实现):

#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) {
        if (&other == this)
            return *this;
        last = other.last;
        return *this;
    }
protected:
    void copy_data (const foo& other) {
        last = other.last;
    }
};

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;
//      foo<Rest...>::operator= (other);
        foo<Rest...>::copy_data(other);
        std::cout << "[Assignment operator called]\n";
        return *this;
    }
protected:
    void copy_data (const foo& other) {
        first = other.first;
        foo<Rest...>::copy_data(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 = b;  // Copy assignment operator works fine (and optimized).
}