今天发现了c++符号重载的一个非常实用的使用实例。

 

在看LiteSQL这个项目的源码的时候发现一段非常有趣的代码:

 

我的第一反应是:string 竟然还可以这样用?随即写了一段测试代码:

 
编译后却得到这样的错误:


$ g++ -o tt tt.cc tt.cc: In function ‘int main(int, char**)’: tt.cc:9: error: no match for ‘operator*’ in ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >(((const char*)"#"), ((const std::allocator<char>&)((const std::allocator<char>*)(& std::allocator<char>())))) * 30’ 由此看来这并不是STL的标准特性,于是想起了C++的符号重载,马上使用 grep: $ grep "operator/*" * -R ... src/library/string.cpp:std::string operator*(int amount, const std::string &s) { src/library/string.cpp:std::string operator*(const std::string &s, int amount) { 这下都明白了:

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值