I am learning C++ and studying Chapter 18 of Vandevoorde and Josuttis's C++ Templates. I retyped their code for Expression Templates, but the following is producing the error
我正在学习C ++并学习Vandevoorde和Josuttis的C ++模板的第18章。我重新输入了表达式模板的代码,但以下是产生错误
sarray1.cpp:36:6: error: ‘T& SArray<T>::operator[](size_t) const’ cannot be overloaded
T& operator[] (size_t idx) const {
^
sarray1.cpp:32:5: error: with ‘T SArray<T>::operator[](size_t) const’
T operator[] (size_t idx) const {
Here's the code:
这是代码:
template <typename T>
class SArray {
public:
...
T operator[] (size_t idx) const {
return storage[idx];
}
T& operator[] (size_t idx) const {
return storage[idx];
}
...
};
I am just learning C++ so I hadn't seen an instance of an overloaded function that differed only be return type, but I see that this is indeed done: Overload a C++ function according to the return value. I also see that []
is not on the list of operators that cannot be overloaded in C++. I can't think of what else could be going wrong. What is the reason for the above error?
我只是在学习C ++,所以我没有看到一个重载函数的实例只是返回类型,但我看到确实已经完成了:根据返回值重载C ++函数。我还看到[]不在C ++中无法重载的运算符列表中。我想不出还有什么可能出错。上述错误的原因是什么?
3 个解决方案
#1
4
You made a mistake when re-typing the code from page 323. Only the first overload should be const
, while the second one should be non-const
:
从第323页重新键入代码时出错。只有第一个重载应该是const,而第二个重载应该是非const:
T operator[] (size_t idx) const {
return storage[idx];
}
T& operator[] (size_t idx) { // <<== No const here
return storage[idx];
}
The point this code is trying to illustrate is that you can overload an operator on const
-ness of this
object. C++ will figure out from the context which of the two operators it should call, and then either return a reference when SArray
is non-const, or return a copy when the array is const
.
这段代码试图说明的一点就是你可以在这个对象的const-ness上重载操作符。 C ++将从上下文中找出它应调用的两个运算符中的哪一个,然后在SArray为非const时返回引用,或者在数组为const时返回副本。
#2
1
Strictly speaking, you can't overload on the return type. If you read the question and answer that you linked (Overload a C++ function according to the return value), you would see that a workaround is required.
严格来说,你不能在返回类型上重载。如果您阅读了链接的问题和答案(根据返回值重载C ++函数),您将看到需要一种解决方法。
However, you really have to ask what you are doing this? Is there a simpler solution?
但是,你真的要问你在做什么吗?有更简单的解决方案吗?
You probably should have one const
method, and one non-const
method:
您可能应该有一个const方法和一个非const方法:
const T& operator[] (size_t idx) const {
return storage[idx];
}
T& operator[] (size_t idx) {
return storage[idx];
}
#3
1
You cannot overload by return type in C++. What's considered are the arguments and, for member functions, the qualification. In this case, I'm guessing what you meant to do was:
您不能通过C ++中的返回类型重载。所考虑的是参数,对于成员函数,还有资格。在这种情况下,我猜你的意思是:
// a non-const member function to return a non-const reference
T& operator[] (size_t idx) {
return storage[idx];
}
// a const member function to return a const reference
const T& operator[] (size_t idx) const {
return storage[idx];
}
This way, the const
member function will be called if this
is a pointer to const
(because the non-const
function is not viable) and the non-const
function will be called otherwise (because it will be a better match).
这样,如果这是一个指向const的指针(因为非const函数不可行),将调用const成员函数,否则将调用非const函数(因为它将是更好的匹配)。
#1
4
You made a mistake when re-typing the code from page 323. Only the first overload should be const
, while the second one should be non-const
:
从第323页重新键入代码时出错。只有第一个重载应该是const,而第二个重载应该是非const:
T operator[] (size_t idx) const {
return storage[idx];
}
T& operator[] (size_t idx) { // <<== No const here
return storage[idx];
}
The point this code is trying to illustrate is that you can overload an operator on const
-ness of this
object. C++ will figure out from the context which of the two operators it should call, and then either return a reference when SArray
is non-const, or return a copy when the array is const
.
这段代码试图说明的一点就是你可以在这个对象的const-ness上重载操作符。 C ++将从上下文中找出它应调用的两个运算符中的哪一个,然后在SArray为非const时返回引用,或者在数组为const时返回副本。
#2
1
Strictly speaking, you can't overload on the return type. If you read the question and answer that you linked (Overload a C++ function according to the return value), you would see that a workaround is required.
严格来说,你不能在返回类型上重载。如果您阅读了链接的问题和答案(根据返回值重载C ++函数),您将看到需要一种解决方法。
However, you really have to ask what you are doing this? Is there a simpler solution?
但是,你真的要问你在做什么吗?有更简单的解决方案吗?
You probably should have one const
method, and one non-const
method:
您可能应该有一个const方法和一个非const方法:
const T& operator[] (size_t idx) const {
return storage[idx];
}
T& operator[] (size_t idx) {
return storage[idx];
}
#3
1
You cannot overload by return type in C++. What's considered are the arguments and, for member functions, the qualification. In this case, I'm guessing what you meant to do was:
您不能通过C ++中的返回类型重载。所考虑的是参数,对于成员函数,还有资格。在这种情况下,我猜你的意思是:
// a non-const member function to return a non-const reference
T& operator[] (size_t idx) {
return storage[idx];
}
// a const member function to return a const reference
const T& operator[] (size_t idx) const {
return storage[idx];
}
This way, the const
member function will be called if this
is a pointer to const
(because the non-const
function is not viable) and the non-const
function will be called otherwise (because it will be a better match).
这样,如果这是一个指向const的指针(因为非const函数不可行),将调用const成员函数,否则将调用非const函数(因为它将是更好的匹配)。