I've written the following code I was going to run through to help me review inheritance and how dispatching/double dispatching works in C++ but it won't compile. I've looked up class prototyping/forward declarations and I've done that but I'm still getting an error "B is an incomplete type", "SubB is an incomplete type", etc. What's the problem?
#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;
}
};
Changing the parameters to references made this work (help from R Sahu) but why doesn't this work now?
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);
When you have a forward declaration, you can use the type only be reference: pointers and references are most obvious references.
Instead of
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;
}
use
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;
}