在C++中是否可以在不编辑创建类的原始源文件的情况下在不同的源文件中扩展类(添加方法)?
在obj-c中,可以通过写另一个来实现 @interface AbcClass (ExtCategory) ... @end
我尝试这些时出现编译时错误:
//Abc.h
class Abc { //This class is from a 3rd party library....
// ...I don't want to edit its source file.
void methodOne();
void methodTwo();
}
//Abc+Ext.h
class Abc { // ERROR: Redefinition of 'Abc'
void methodAdded();
}
Run Code Online (Sandbox Code Playgroud)
如何在不创建派生类的情况下添加方法?我的目标是保留'Abc'名称并为其添加方法.我使用的第三方库中的特定类缺少一些方法,我想添加这些方法,但我保持源文件未编辑.
有没有办法做到这一点?我是编写C++代码的新手.我熟悉它的一些语法,但不太了解.
c++ ×1