我有一个父类,子类,在这个子类中,我定义了一些结构。在这个结构中,我想调用父方法。有可能吗?
class Parent
{
public:
int foo(int x);
}
class Child : public Parent
{
public:
struct ChildStruct {
int x;
int bar(int y) {
return GET_CLASS_CHILD->foo(this->x + y);
}
};
}
在C++中这样的事情可能发生吗?那又如何实现呢?
发布于 2015-04-16 03:02:49
您必须向ChildStruct
传递指向所有者类实例的引用或指针:
class Child : public Parent
{
public:
struct ChildStruct {
int x;
Child& owner;
ChildStruct(Child& owner_) : owner(owner_) {}
int bar(int y) {
return owner.foo(this->x + y);
}
};
};
也就是说,看起来您真正需要的是一个lambda function。
https://stackoverflow.com/questions/29672588
复制