I am writing code that follows the member dependency schemes discussed in:
Resolve build errors due to circular dependency amongst classes
In it's simplest form, the code headers looks like this:
A.h
class B;
class A{
public:
A();
void send(int i);
B* b;
};
B.h
#include A.h
#include foo.h
class B : public foo{
public:
B(A *a);
A *a;
void send(int i); //this invokes a member function of a
};
Now, in my source code, if I use an initializer list that looks like:
A.cpp
A::A() : b(this){};
I get a compiler error saying "error: cannot convert ‘A* const’ to ‘B*’ in initialization
However, if I don't use an initializer list, and instead I use a new pointer declaration, it compiles fine:
A::A(){
b = new B(this);
}
Could anyone help me reason why the initializer list causes the compiler to misinterpret the class B constructor argument? Or maybe something else is happening?
Just in case it helps, here is the B.cpp file:
B.cpp
#include "B.h"
#include "A.h"
B::B(A *inst){
a = inst;
}
void B::send(int i){
a->send(i);
}