C++中括号()操作符的重载举例及解析
2015-05-25 19:15
239 查看
C++项目中,经常会遇到使用重载()操作符的地方,请看下面一个例子:
运行结果如下:
[cpp] view
plaincopy
@-desktop:~/test$ g++ -o o 6.cpp
@-desktop:~/test$ ./o
Hello Clastype!1
Hello Clastype!2
Hello Clastype!3
Hello Clastype()!4
Hello Clastype!5
Hello Clastype()!6
#include <iostream> using namespace std; class Clastype { public: Clastype(int a) { cout << "Hello Clastype!" << a << endl; } bool operator ()(int b) { cout << "Hello Clastype()!" << b << endl; return true; } }; int main() { Clastype a(1); Clastype(2); Clastype t = Clastype(3); t(4); Clastype *b = new Clastype(5); (*b)(6); }
运行结果如下:
[cpp] view
plaincopy
@-desktop:~/test$ g++ -o o 6.cpp
@-desktop:~/test$ ./o
Hello Clastype!1
Hello Clastype!2
Hello Clastype!3
Hello Clastype()!4
Hello Clastype!5
Hello Clastype()!6
相关文章推荐
- C++中括号()操作符的重载举例及解析
- C++中括号()操作符的重载举例及解析
- C++中括号()操作符的重载举例及解析
- C++中括号()操作符的重载举例及解析
- C++中括号()操作符的重载举例及解析
- C++中括号()操作符的重载自我理解
- C++中括号()操作符的重载举例及解析
- C++常见操作符的重载
- C++中重载输入输出操作符
- C++重载箭头操作符
- 重载c++的<<操作符
- C++中重载+操作符的正确方法
- c++ 重载 >>(输入) 、<< (输出) 操作符
- c++ 操作符 重载。
- C++中重载、覆盖、隐藏解析
- C++操作符重载
- C++ 指针悬挂和赋值操作符的重载,拷贝构造函数实现
- C++中重载数组下标访问操作符[ ] 和 赋值操作符 = ,* 和 -> 操作符和取反 !以及转化操作符bool和void*
- C/C++ 之 ‘++’、‘+’、‘+=’、‘-’、‘()’、‘--’ 操作符的重载实现重载
- C++_函数的重载使用举例