C++ 中 类 的前向声明
Forward Declaration of Classes in C++
我已经编写了下面的代码,我打算 运行 通过它来帮助我回顾继承以及 dispatching/double 调度在 C++ 中的工作方式,但它无法编译。我查看了 class prototyping/forward 声明,我已经这样做了,但我仍然收到错误 "B is an incomplete type"、"SubB is an incomplete type" 等。有什么问题?
#include <iostream>
class B;
class SubB;
class A {
public:
void talkTo(B b){
std::cout << "A talking to instance of B" << std::endl;
}
void talkTo(SubB sb){
std::cout << "A talking to instance of SubB" << std::endl;
}
};
class SubA : A {
public:
void talkTo(B b){
std::cout << "SubA talking to instance of B" << std::endl;
}
void talkTo(SubB sb){
std::cout << "SubA talking to instance of SubB" << std::endl;
}
};
class B {
public:
void talkTo(A a){
std::cout << "B talking to instance of A" << std::endl;
}
void talkTo(SubA sa){
std::cout << "B talking to instance of SubA" << std::endl;
}
};
class SubB : B {
public:
void talkTo(A a){
std::cout << "SubB talking to instance of A" << std::endl;
}
void talkTo(SubA sa){
std::cout << "SubB talking to instance of SubA" << std::endl;
}
};
编辑
将参数更改为引用使这项工作(来自 R Sahu 的帮助)但为什么现在不工作?
class A {
public:
void talkTo(B &b){
//std::cout << "A talking to instance of B" << std::endl;
b.talkTo(this);
}
void talkTo(SubB &sb){
//std::cout << "A talking to instance of SubB" << std::endl;
sb.talkTo(this);
}
};
class B {
public:
void talkTo(A &a){
std::cout << "B talking to instance of A" << std::endl;
}
void talkTo(SubA &sa){
std::cout << "B talking to instance of SubA" << std::endl;
}
};
class SubB : B {
public:
void talkTo(A &a){
std::cout << "SubB talking to instance of A" << std::endl;
}
void talkTo(SubA &sa){
std::cout << "SubB talking to instance of SubA" << std::endl;
}
};
A a;
SubA subA;
B b;
SubB subB;
a.talkTo(b);
a.talkTo(subB);
当你有前向声明时,你只能使用引用类型:指针和引用是最明显的引用。
而不是
void talkTo(B b){
std::cout << "A talking to instance of B" << std::endl;
}
void talkTo(SubB sb){
std::cout << "A talking to instance of SubB" << std::endl;
}
使用
void talkTo(B const& b){
std::cout << "A talking to instance of B" << std::endl;
}
void talkTo(SubB const& sb){
std::cout << "A talking to instance of SubB" << std::endl;
}
我已经编写了下面的代码,我打算 运行 通过它来帮助我回顾继承以及 dispatching/double 调度在 C++ 中的工作方式,但它无法编译。我查看了 class prototyping/forward 声明,我已经这样做了,但我仍然收到错误 "B is an incomplete type"、"SubB is an incomplete type" 等。有什么问题?
#include <iostream>
class B;
class SubB;
class A {
public:
void talkTo(B b){
std::cout << "A talking to instance of B" << std::endl;
}
void talkTo(SubB sb){
std::cout << "A talking to instance of SubB" << std::endl;
}
};
class SubA : A {
public:
void talkTo(B b){
std::cout << "SubA talking to instance of B" << std::endl;
}
void talkTo(SubB sb){
std::cout << "SubA talking to instance of SubB" << std::endl;
}
};
class B {
public:
void talkTo(A a){
std::cout << "B talking to instance of A" << std::endl;
}
void talkTo(SubA sa){
std::cout << "B talking to instance of SubA" << std::endl;
}
};
class SubB : B {
public:
void talkTo(A a){
std::cout << "SubB talking to instance of A" << std::endl;
}
void talkTo(SubA sa){
std::cout << "SubB talking to instance of SubA" << std::endl;
}
};
编辑
将参数更改为引用使这项工作(来自 R Sahu 的帮助)但为什么现在不工作?
class A {
public:
void talkTo(B &b){
//std::cout << "A talking to instance of B" << std::endl;
b.talkTo(this);
}
void talkTo(SubB &sb){
//std::cout << "A talking to instance of SubB" << std::endl;
sb.talkTo(this);
}
};
class B {
public:
void talkTo(A &a){
std::cout << "B talking to instance of A" << std::endl;
}
void talkTo(SubA &sa){
std::cout << "B talking to instance of SubA" << std::endl;
}
};
class SubB : B {
public:
void talkTo(A &a){
std::cout << "SubB talking to instance of A" << std::endl;
}
void talkTo(SubA &sa){
std::cout << "SubB talking to instance of SubA" << std::endl;
}
};
A a;
SubA subA;
B b;
SubB subB;
a.talkTo(b);
a.talkTo(subB);
当你有前向声明时,你只能使用引用类型:指针和引用是最明显的引用。
而不是
void talkTo(B b){
std::cout << "A talking to instance of B" << std::endl;
}
void talkTo(SubB sb){
std::cout << "A talking to instance of SubB" << std::endl;
}
使用
void talkTo(B const& b){
std::cout << "A talking to instance of B" << std::endl;
}
void talkTo(SubB const& sb){
std::cout << "A talking to instance of SubB" << std::endl;
}