返回类型不匹配(或不匹配)
Return type mismatch (or not)
我对这段代码有点困惑。这实际上是我的,但我仍然不明白为什么这个编译没有任何警告。
#include <iostream>
class Line {
private:
int length;
public:
Line(void);
Line(int);
int getLength(void);
Line& operator = (const Line&);
};
Line::Line(int a) : length(a) {}
int Line::getLength(void) { return length; }
Line& Line::operator = (const Line& line) // The function return type is reference of a value of the Line type.
{
length = line.length;
return *this; // The function actually returns dereferenced (*this) value.
}
int main(void)
{
Line line {2};
Line line_a {0};
line_a = line;
std::cout << line_a.getLength() << std::endl;
return 0;
}
唯一的区别是没有&号。它仍然会编译
#include <iostream>
class Line {
private:
int length;
public:
Line(void);
Line(int);
int getLength(void);
Line operator = (const Line&);
};
Line::Line(int a) : length(a) {}
int Line::getLength(void) { return length; }
Line Line::operator = (const Line& line) // The function return type is reference of a value of the Line type.
{
length = line.length;
return *this; // The function actually returns dereferenced (*this) value.
}
int main(void)
{
Line line {2};
Line line_a {0};
line_a = line;
std::cout << line_a.getLength() << std::endl;
return 0;
}
this
是指向当前对象的指针。解引用this
就得到了那个对象。当函数的返回类型是引用而您返回一个对象时,将返回对该对象的引用。所以,你正在返回一个对你从解引用this
指针得到的对象的引用。没有任何警告的理由。
如果你没有解引用this
指针,那么你会得到一个编译错误,因为你会试图返回(引用)一个指针,这将与函数的返回类型冲突。
让我们考虑这段代码:
#include <iostream>
int global_variable = 8;
int function(void)
{
return global_variable;
}
int main(void)
{
function() = 16; // Compiling this code would give error: lvalue required as left operand of assignment.
std::cout << global_variable << std::endl;
return 0;
}
但是,如果我们稍微修改一下这段代码,将函数返回类型更改为引用int -这将把lvalue
更改为rvalue
(int somenumber
是lvalue
, int& somenumber
是rvalue
)。
#include <iostream>
int global_variable = 8;
int& function(void)
{
return global_variable;
}
int main(void)
{
function() = 16;
std::cout << global_variable << std::endl; // The global_variable becomes 16! (it was 8);
return 0;
}
相关文章:
- 与'operator='不匹配(操作数类型'String'且"void")
- 错误:'operator>'不匹配(操作数类型为 'Student' 和 'Student'
- 与"运算符>>"不匹配(操作数类型为"QDataStream"和"QJsonObject")
- 与'operator<<'不匹配(操作数类型'std::ostream {aka std::basic_ostream<char>}'
- 与"operator<<"不匹配(操作数类型'std::ostream'
- 比较 C++ 中的两个数组,并根据元素的匹配或不匹配返回值
- C++ 重载:[错误] 与'operator='不匹配(操作数类型为 'String' 和"字符串")
- 错误:与"运算符>>"不匹配(操作数类型为"std::istream
- 与"运算符[]"不匹配(操作数类型为"std::unique_ptr<std::vector<int> >"和"int")
- 错误:与'operator='不匹配(操作数类型为 'PlDrustvo' 和"PlDrustvo")|
- 错误:与'operator='不匹配(操作数类型为"std::map<int、double>::iterator
- \main.cpp|103|error: 与"operator<<"不匹配(操作数类型为"std::ostream {aka std::basic_ostream<char&g
- 错误:与"运算符[]"不匹配(操作数类型为"QVector<int>"和"QCharRef")
- 从保存变量数据类型的数据结构中检索值,而不指定返回类型
- 有没有办法修复此错误:与'operator[]'不匹配(操作数类型为 'empl' 和"int")
- 错误与'operator='不匹配(操作数类型为 'Person' 和"Person*")
- 运算符重载时出错(错误:"运算符<<不匹配(操作数类型为"std::basic_ostream<char>"和"const char [2]")
- 与"运算符[]"不匹配(操作数类型为"Briques"和"int")
- 与'operator[]'不匹配(操作数类型为 'QVariant' 和"常量字符 [2]") QVariant/QVariantMap
- C++错误 C2533,ctor :构造函数不允许返回类型