作者Carbontube (碳管)
看板C_and_CPP
标题Re: [问题] operator overloading c++
时间Thu May 14 20:21:45 2009
看书说,C++不能定义新的operator。
好比想把 pow(a,n) 写成 a**n 是不可行的
那请问板上大牛是如何实现的呢
或是通常会怎处理呢,谢谢
※ 引述《Fenikso (ばかちーは俺の嫁)》之铭言:
: 又到了写低能code的时间~
: 以下VS2008限定, 2005或更早的可能不行
: gcc的-fextended-identifiers还是个半成品(敲碗)
: #define ! + 阶乘()
: /* 这是全形的"!" 不是"!" */
: struct 阶乘{};
: int operator+(int x, const 阶乘& y) {
: int j = 1;
: for(int i=1; i<=x; i++) j *= i;
: return j;
: }
: int main() {
: cout << 5! << endl;
: int a = 6;
: cout << a ! << endl; // a和!中间要有空格 不然会被当成一个identifier
: return 0;
: }
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.112.80.230
1F:推 sunneo:乾脆写个preprocessor让自订operator合理化 05/14 22:13
2F:推 ianfang:实做不是问题 问题在可读性 05/14 22:45