为什么模板运算符重载不起作用?
why template operator overloading doesn't work?
为什么这个代码不打印"operator="?
#include <iostream>
using namespace std;
class A{
public:
template<typename T> void operator=(const T& other){
cout<<"operator="<<endl;
}
};
int main(){
A a;
A b;
a=b;
}
编译器生成的副本分配运算符由重载解析选择:
class A{
public:
A& operator=(A const& other){
std::cout << "copy assignmentn";
return *this;
}
template<class T>
void operator=(T const& other){
std::cout << "templated assignmentn";
}
};
将打印"副本分配",基本上等于编译器为您生成的内容(当然,没有打印)。
相关文章:
- 重载运算符 (<<) cout 在 c ++ 中不起作用,当我互相减去两个矩阵时不起作用
- 为什么重载运算符>在参数声明 const 时不起作用?
- 重载"+="运算符:c+=a_times_b 有效,但 c+=a*b 不起作用?
- = 重载不起作用
- 类中的重载函数(Students())是输出输入的信息,但它不起作用
- 为什么 SFINAE 在这个简单的成员函数重载中不起作用
- C++ 运算符重载 += 有效,但<<不起作用
- 需要使用 Book* 头变量重载运算符,但不起作用
- C++ 运算符删除重载对派生类不起作用
- 为什么重载运算符上的异常说明符'<<'不适用于任何 std::ostream 对象,但对库中定义的运算符不起作用?
- 运算符重载如何工作,为什么在我的情况下不起作用?
- 为什么重载运算符不起作用?
- 重载'='不起作用
- 二叉搜索树<<运算符重载不起作用
- 重载运算符<<不起作用
- 重新赋值在运算符中不起作用 = 重载
- 类构造函数重载不起作用
- c++中的函数重载不起作用
- 为什么 [] 的重载函数在这里不起作用?
- 如果重载函数仅被部分重写,则多态性不起作用