有没有办法在运行时向类添加一个字段(以前不存在的字段)?像这样的片段:
Myobject *ob; // create an object
ob->addField("newField",44); // we add the field to the class and we assign an initial value to it
printf("%d",ob->newField); // now we can access that field
我真的不在乎它会如何完成,我不在乎它是否是一个丑陋的黑客,我想知道它是否可以完成,如果可能的话,还有一个小例子。
另一个例子:假设我有一个描述这个类的 XML 文件:
<class name="MyClass">
<member name="field1" />
<member name="field2" />
</class>
我想将字段“field1”和“field2”“添加”到类中(假设类已经存在)。假设这是该类的代码:
class MyClass {
};
我不想在运行时创建一个类,我只想将成员/字段添加到现有的。
谢谢 !