C ++类扩展 [英] C++ Class Extension

查看:98
本文介绍了C ++类扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法添加新的方法到类,无需修改原始类定义(即编译的.lib包含类和相应的.h文件)像C#的类扩展方法?

Is there a way to add new methods to a class, without modifying original class definition (i.e. compiled .lib containing class and corresponding .h file) like C#'s class extension methods?

推荐答案

否。 C ++没有这样的能力。

No. C++ has no such capability.

如其他答案所述,常见的解决方法是:

As mentioned in other answers, the common workarounds are:

  • Define a derived class, perhaps with a factory to hide the actual implementation class
  • Define a decorator class
  • Define functions that operate on instances of the class

这篇关于C ++类扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆