开发者

typedef and operator overloading in C++

Suppose I typedef an integer or integer array or any known type:

typedef int int2

Then I overload operat开发者_JAVA百科or * for int2 pairs, now if I initialize variables a and b as int. Then will my * between a and b be the overloaded * ?

How do I achieve overloading an int and yet also use * for int the way they are. Should I create a new type?


Assuming you are talking about C++:
Operator overloads have to take at least one argument of user-defined type. The typedef doesn't change anything, as it does not introduce a new type and only provides a synonym.


What you need is a Strong Typedef.

Boost offered version that should work for you, or at least help you resolve your need :

http://www.boost.org/doc/libs/1_42_0/boost/strong_typedef.hpp


C does not allow operator overloading.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜