在 parent class cpp 中使用 child 函数
Use a child function in a parent class cpp
我有一个 parent class 和一个 child class 其中 parent class 使用 child classes 在 parent 的方法之一中。在这个方法中,parent 使用了 child 的构造函数和一个方法(即从 parent 继承的虚方法),我在 stackoeverflow 上看到了另一个与此类似的问题,但他们做到了不使用 child 的使用方法和构造函数(有人提到问题更容易,因为他们只使用 child-class 的变量)。我在 parent 中尝试了一些基本的 class 转发(将 class child; 放在顶部)但没有用。
这是没有任何 public 私人区别的设置尝试解决所需 headers:
//foo.h
class Foo{
int x;
Foo(int i);
virtual void funA ();
void funB();
};
//foo.cpp
Foo::Foo(int i) {
x = i;
}
Foo::funA(){cout<<"Foo funA"<<endl;}
Foo::funB(){
Bar b = Bar();
b.funA();
}
//bar.h
class Bar : public Foo {
Bar(int i);
virtual void funA ();
};
//bar.cpp
Bar::Bar(int i) { x = i };
void Bar::funA(){cout<<"Bar funA"<<endl;}
我似乎无法让 class 转发正常工作。如果有人能告诉我如何设置我的包含和 class 转发,那就太好了!
Foo.h
class Foo {
int x;
Foo(int i);
virtual void funA();
void funB();
}
Foo.cpp
#include "Foo.h"
#include "Bar.h"
...
void Foo::funB() {
Bar b();
b.funA();
}
...
Bar.h
依赖于 Foo.h
但您不需要 classes 的任何前向声明(例如,当您有像 class Foo;
这样的行时没有定义 class 主体),只是普通的声明。这是因为 Foo
没有被 Bar
的接口使用,只有它的实现,所以你的 Bar.h
文件可以保持不变。
我有一个 parent class 和一个 child class 其中 parent class 使用 child classes 在 parent 的方法之一中。在这个方法中,parent 使用了 child 的构造函数和一个方法(即从 parent 继承的虚方法),我在 stackoeverflow 上看到了另一个与此类似的问题,但他们做到了不使用 child 的使用方法和构造函数(有人提到问题更容易,因为他们只使用 child-class 的变量)。我在 parent 中尝试了一些基本的 class 转发(将 class child; 放在顶部)但没有用。
这是没有任何 public 私人区别的设置尝试解决所需 headers:
//foo.h
class Foo{
int x;
Foo(int i);
virtual void funA ();
void funB();
};
//foo.cpp
Foo::Foo(int i) {
x = i;
}
Foo::funA(){cout<<"Foo funA"<<endl;}
Foo::funB(){
Bar b = Bar();
b.funA();
}
//bar.h
class Bar : public Foo {
Bar(int i);
virtual void funA ();
};
//bar.cpp
Bar::Bar(int i) { x = i };
void Bar::funA(){cout<<"Bar funA"<<endl;}
我似乎无法让 class 转发正常工作。如果有人能告诉我如何设置我的包含和 class 转发,那就太好了!
Foo.h
class Foo {
int x;
Foo(int i);
virtual void funA();
void funB();
}
Foo.cpp
#include "Foo.h"
#include "Bar.h"
...
void Foo::funB() {
Bar b();
b.funA();
}
...
Bar.h
依赖于 Foo.h
但您不需要 classes 的任何前向声明(例如,当您有像 class Foo;
这样的行时没有定义 class 主体),只是普通的声明。这是因为 Foo
没有被 Bar
的接口使用,只有它的实现,所以你的 Bar.h
文件可以保持不变。