假设你有:
struct c_struct {
int value;
/* other stuff */
void (* dump)();
};
并且您希望在某个时候:
c_struct_obj->dump();
我认为你无法实例化一个 c_struct 对象,使其特定的“dump”函数知道其特定的“值”,就像 C++ 方法知道成员变量的方式(我想是通过隐式的“this”)?我想我已经知道答案了(“不”)。如果是这样,是否还有其他方式以 OOPy 方式使用 C 结构体?
最佳答案
当然,您只需自己传递this
即可:
struct c_struct {
int value;
/* other stuff */
void (* dump)(struct c_struct *this);
};
然后用以下方式调用它:
c_struct_obj->dump(c_struct_obj);
关于c++ - 仅使用 C 结构并保持 OOPy?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3971554/