C++ 具有多重继承的复制赋值运算符
下面的复制构造函数工作正常,但我不明白复制赋值运算符有什么问题C++ 具有多重继承的复制赋值运算符,c++,c++11,templates,inheritance,assignment-operator,C++,C++11,Templates,Inheritance,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 fo
#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.
}
#包括
模板类foo;
模板
福班{
最后;
公众:
foo(Last r):Last(r){}
foo()=默认值;
foo(constfoo&other):last(other.last){
foo&运算符=(const foo&其他){
last=other.last;
归还*这个;
}
};
模板
foo类:公共foo{
首先;
公众:
foo(First f,Rest…Rest):foo(Rest…,First(f){}
foo()=默认值;
foo(constfoo&other):foo(other),first(other.first){std::cout您的返回语句
return foo<Rest...>::operator= (other);
派生类中来自运算符=
的返回似乎不正确:
return foo<Rest...>::operator= (other);
多亏了StoryTeller,这里有一个优化的完全编译解决方案(operator=委托给另一个命名成员名copy_data
,该解决方案不检查自我分配,并递归实现):
#包括
模板类foo;
模板
福班{
最后;
公众:
foo(Last r):Last(r){}
foo()=默认值;
foo(constfoo&other):last(other.last){
foo&运算符=(const foo&其他){
如果(&其他==此)
归还*这个;
last=other.last;
归还*这个;
}
受保护的:
无效副本_数据(常量foo和其他){
last=other.last;
}
};
模板
foo类:公共foo{
首先;
公众:
foo(First f,Rest…Rest):foo(Rest…,First(f){}
foo()=默认值;
foo(const foo&other):foo(other),first(other.first){std::cout@Story Teller是的,的确如此。作为奖励,if(&other==this)return*this;
被多次调用,但冗余,对不对(只需调用一次)?但没有办法避免这种情况?@prestokeys-您可以始终将运算符=
委托给另一个不检查的命名成员(并以递归方式实现).@StoryTeller Answer接受了,我在一个单独的答案中实现了你的优化想法。谢谢。@prestokeys你可能不想递归实现任何东西。改用包扩展。在这种情况下可以节省大量模板膨胀。为什么不使用复制和交换?你希望t
不是交换的吗有能力或可测量的性能问题?
foo& operator= (const foo& other) { // Copy assignment operator
if (&other == this)
return *this;
first = other.first;
foo<Rest...>::operator= (other);
return *this;
}
return foo<Rest...>::operator= (other);
this -> foo<Rest...>::operator= (other);
return *this;
#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).
}