Coming from a Java/C# background and need a bit of help understanding what is happening here in C++...
class A {
int x;
public:
A(int x) : x(x){}
void f(int y) {
cout << x + y << endl;
}
};
class B : virtual A {
int x;
public:
B(int x) : A(2*x), x(x) {}
virtual void f(int y){
cout << x + 2*y << endl;
}
};
void h(){
B b(5);
A &a = dynamic_cast<A &>(b);
a.f(10);
b.f
}
void g() {
A *a = this;
a->f(10);
B *b = dynamic_cast<B *>(a);
b->f(10);
}
Calling h() is ok but calling g() will not work. Can someone explain why? Also, in the line A(int x) : x(x){} what does : x(x){} do? Same question for B(int x) : A(2*x), x(x) and : A(2*x), x(x).
Thanks so much in advance for your help.