C++扩展方法
2018-01-05 11:15
1071 查看
在 stackoverflow 看到了一点精华,想要记录下来。
这是关于 C++ 扩展方法的思路:
1.使用定义operator来连接一个struct并调用struct的构造,这个struct的构造即为扩展方法的实现。
(个人感觉不利于扩展方法中多参数,重载的实现。这部分代码不贴出来了,大家根据提供的思路很快也能架构出原型)
使用方法:var result = a | extension::trim();
2.使用扩展class来接收需要扩展方法的对象,接着在扩展class中实现扩展方法。
(利于扩展方法多参数的传递,使用时要外包一层)
使用方法:var result = extension(a).trim();
3.使用定义operator来连接一个扩展class,扩展class接收需要扩展方法的对象,接着在扩展class中实现扩展方法。
使用方法:var result = a<-trim();
#include <iostream>
using namespace std;
class regular_class {
public:
void simple_method(void) const {
cout << "simple_method called." << endl;
}
};
class ext_method {
private:
// arguments of the extension method
int x_;
public:
// arguments get initialized here
ext_method(int x) : x_(x) {
}
// just a dummy overload to return a reference to itself
ext_method& operator-(void) {
return *this;
}
// extension method body is implemented here. The return type of this op. overload
// should be the return type of the extension method
friend const regular_class& operator<(const regular_class& obj, const ext_method& mthd) {
cout << "Extension method called with: " << mthd.x_ << " on " << &obj << endl;
return obj;
}
};
int main()
{
regular_class obj;
cout << "regular_class object at: " << &obj << endl;
obj.simple_method();
obj<-ext_method(3)<-ext_method(8);
return 0;
}
4.看了那么多是不是已经知道C++没有扩展方法了。
C++根本不需要扩展方法,因为用它能实现扩展方法。
这是关于 C++ 扩展方法的思路:
1.使用定义operator来连接一个struct并调用struct的构造,这个struct的构造即为扩展方法的实现。
(个人感觉不利于扩展方法中多参数,重载的实现。这部分代码不贴出来了,大家根据提供的思路很快也能架构出原型)
使用方法:var result = a | extension::trim();
2.使用扩展class来接收需要扩展方法的对象,接着在扩展class中实现扩展方法。
(利于扩展方法多参数的传递,使用时要外包一层)
使用方法:var result = extension(a).trim();
3.使用定义operator来连接一个扩展class,扩展class接收需要扩展方法的对象,接着在扩展class中实现扩展方法。
使用方法:var result = a<-trim();
#include <iostream>
using namespace std;
class regular_class {
public:
void simple_method(void) const {
cout << "simple_method called." << endl;
}
};
class ext_method {
private:
// arguments of the extension method
int x_;
public:
// arguments get initialized here
ext_method(int x) : x_(x) {
}
// just a dummy overload to return a reference to itself
ext_method& operator-(void) {
return *this;
}
// extension method body is implemented here. The return type of this op. overload
// should be the return type of the extension method
friend const regular_class& operator<(const regular_class& obj, const ext_method& mthd) {
cout << "Extension method called with: " << mthd.x_ << " on " << &obj << endl;
return obj;
}
};
int main()
{
regular_class obj;
cout << "regular_class object at: " << &obj << endl;
obj.simple_method();
obj<-ext_method(3)<-ext_method(8);
return 0;
}
4.看了那么多是不是已经知道C++没有扩展方法了。
C++根本不需要扩展方法,因为用它能实现扩展方法。
相关文章推荐
- 高效开发Python扩展模块方法:用SWIG封装C/C++代码
- 高效开发Python扩展模块方法:用SWIG封装C/C++代码
- node.js 和 node-webkit C++扩展方法
- PHP扩展封装Protobuf反序列化方法(c++)
- Python 扩展使用 C/C++ 给 Python 写扩展模块的方法
- python 嵌入 C/C++方法
- c#扩展方法奇思妙用变态篇二:封装 if/else、swith/case及while
- Linux下部署Memcached和PHP的Memcache扩展方法
- 设计模式 C++版:第二十三式 模板方法
- 用boost.python为python写c/c++扩展曲折配置最终成功历程
- jQuery源码学习之七 (jQuery中扩展方法)
- c++ 泛型算法 numeric方法实现原型
- C/C++结构体大小的计算方法(Windows 32为处理器)
- 内核扩展数据包的方法之-----skb_copy_expand
- C++中创建对象的两种方法及其区别
- C++字符串旋转3种方法实现
- C++ 实现类似java c#List的方法 boost::any的使用
- 数据结构中十种经典排序方法汇总(C++)
- 关于C#扩展方法介绍
- C/C++项目调用外部exe程序方法