【发布时间】:2014-05-21 06:33:36
【问题描述】:
我有这个代码:
class ABC{
public:
ABC(std::ostream& os) : _os(os) {}
void operator() (const Stud* course) {
Stud->print(_os);
}
ABC& operator=(const ABC& other); //Declaration of operator=
private:
std::ostream& _os;
};
在我定义赋值运算符之前,我收到警告“无法生成赋值运算符”。 我想在对象之间分配这个类什么都不做,它只是在 for_each 算法中打印类。
我试着写:
ABC& operator=(const ABC& other){
return *this;
}
但仍会收到其他不使用的警告。 我如何定义赋值运算符什么都不做。 (不使用#pragma 警告语句来抑制警告)。
谢谢!
【问题讨论】:
-
赋值运算符的主体是在哪里写的?
-
我写了内联赋值运算符的主体
-
我也写了这个 ABC& operator=(const ABC& ){ return *this; } 它的工作,但我不知道它是否正常
-
我明白了,但我不明白为什么要编写一个什么都不做的赋值运算符......它不会分配任何东西,对吧?
-
因为我需要这个类来完成其他任务,并且没有赋值运算符我会收到警告
标签: c++ class reference warnings