![](/img/trans.png)
[英]Return type of CallStaticBooleanMethodV does not match Java method signature - Calling Java method from C/C++
[英]C++ identical method signature but different return type
我看过以下代码:
template <class T>
class Type {
public:
Type() {}
T& operator=(const T& rhs) {value() = rhs; return value();}
T& value() {return m_value;}
T value() const {return m_value;}
private:
T m_value;
};
为什么编译器不抱怨
T& value() {return m_value;}
T value() const {return m_value;}
以及如何知道调用哪一个?
这两个功能实际上并不相同。 只有第二个函数被声明为const
成员函数。 如果调用该成员的对象是const
,则使用后一个选项。 如果对象是非const
,则使用第一个选项。
例:
void any_func(const Type *t)
{
something = t->value(); //second `const` version used
}
void any_func2(Type *t)
{
something = t->value(); //first non-`const` version used
}
如果这两个函数被宣布不const
或两者被宣布为const
,编译器会 (应,反正)抱怨。
为什么编译器不抱怨
因为const
计算不同的函数签名。 你假设函数签名是相同的是错误的。
标记为const
将为Type<T>
任何const
实例或引用调用。
以及如何知道调用哪一个?
在函数中放置一个cout
语句并测试以下情况:
template <class T>
class Type {
public:
Type() {}
T& operator=(const T& rhs) {value() = rhs; return value();}
T& value() {
std::cout << "non const version" << std endl;
return m_value;
}
T value() const {
std::cout << "const version" << std endl;
return m_value;
}
private:
T m_value;
};
int main() {
Type<int> t;
t.value();
Type<int> rt = t;
rt.value();
Type<int>* pt = &t;
pt->value();
const Type<int> ct;
ct.value();
const Type<int>& crt = t;
crt.value();
const Type<int>* pct = &t;
pct->value();
}
赋值运算符将调用非const版本。
const版本应该更好看
const T& value() const {
std::cout << "const version" << std endl;
return m_value;
}
因为您不能总是依赖RVO(返回值优化),并且可能会采用额外的副本(特别是对于较旧的编译器实现)。
另请注意赋值运算符应返回对当前实例的引用:
Type& operator=(const T& rhs) {value() = rhs; return *this;}
关于功能解析优先级的几个词。 编译器通过以下方式区分const /非const函数:
如果一个类只有const函数和给定的名称和参数列表,那么它将被调用为常量和非常量对象。 在调用此函数之后,对象将“假定”constness(即使它不是const),这意味着该函数只能调用其他const函数。
如果一个类只有非const函数,那么它将被调用非const对象。 尝试为const对象调用此函数将导致编译错误。
如果一个类有两个可用的函数,const版本将用于const对象,非const版本将用于非const对象。
感谢@owacoder将我的注意力集中在描述中的初始混音上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.