由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - ambiguous operators in c++
相关主题
ostream& operator << (ostream& s, int cnt) errortemplate
member and friendthrust help ~~~
[C++] 入门级问题 increment and decrement operators求题目
overload "++i"里的operator“++”,怎么declare?求题目
问个overloading new operator的问题请教boost::any compile错误。
C++命名空间和算子重载why copy assignment operator returns non-const type?
string operator +C++ func overload question
一个c++小问题why use static function here?
相关话题的讨论汇总
话题: operator话题: unsigned话题: operators话题: c++话题: ambiguous
进入Programming版参与讨论
1 (共1页)
p****o
发帖数: 1340
1
i have a question on operators in c++.
/* header file */
class A;
class B
{
public:
operator A*();
double& operator [](unsigned int);
};
/* cpp file */
B b;
b[3] = 2.2;
the compiler will complain about the "b[3]=2.2" line b/c there are two
possible ways to interpret the code:
1. b.operator[](unsigned) -- my desired operation
2. b.operator A*.operator[] -- which is build-in.
the above code can be compiled and run well under linux, but now i am using
g++ under windows. i just realize suc
p***o
发帖数: 1252
2
It's in 13.3, though I didn't read it ...

【在 p****o 的大作中提到】
: i have a question on operators in c++.
: /* header file */
: class A;
: class B
: {
: public:
: operator A*();
: double& operator [](unsigned int);
: };
: /* cpp file */

p****o
发帖数: 1340
3
yes, the strictness of the compiler can expose many un-imaginable problems
in the code. it actually takes me quite some time to figure out the error
message because the real code is much more complicated than the sample given.

【在 p***o 的大作中提到】
: It's in 13.3, though I didn't read it ...
p***o
发帖数: 1252
4
Oh, yes. But implementing operator T* for [] is strange enough.

given.

【在 p****o 的大作中提到】
: yes, the strictness of the compiler can expose many un-imaginable problems
: in the code. it actually takes me quite some time to figure out the error
: message because the real code is much more complicated than the sample given.

r*********r
发帖数: 3195
5
shouldn't g++ pick "operator []"? the other path requires more "effort".
p****o
发帖数: 1340
6
in my code, T* is an abstract opaque pointer due to the system design.
while [] is an operator for accessing the real data. so they both make
sense somehow.
now the problem is solved. i declare the operator [] as
double& operator [] (int);
originally i used "unsigned int" for the most strict type checking. but in
practice, the compiler treated all constants like 5 as int but not unsigned.
so there is a conversion from int to unsigned. that's why the compiler does
not know which decoding path to

【在 p***o 的大作中提到】
: Oh, yes. But implementing operator T* for [] is strange enough.
:
: given.

i***h
发帖数: 12655
7
[] by default is pointer reference
I think this way is more elegant than overloading [].
[] overloading can be used for more complicated index operation.

【在 p***o 的大作中提到】
: Oh, yes. But implementing operator T* for [] is strange enough.
:
: given.

1 (共1页)
进入Programming版参与讨论
相关主题
why use static function here?问个overloading new operator的问题
一个inheritance 的问题C++命名空间和算子重载
question overloading ++ errorstring operator +
one question about overloading operator delete一个c++小问题
ostream& operator << (ostream& s, int cnt) errortemplate
member and friendthrust help ~~~
[C++] 入门级问题 increment and decrement operators求题目
overload "++i"里的operator“++”,怎么declare?求题目
相关话题的讨论汇总
话题: operator话题: unsigned话题: operators话题: c++话题: ambiguous