K
Karsten Hochkirch
Hi,
I have just encountered a problem when calling a virtual member function
in a constructor.
Only the memberfunction of the parent class is called instead of the
overloaded function.
I have attached a small example.
Is that bug or feature. If feature: is there an easy way to get the
behaviour as I am looking for?
Thanks
Karsten
#include <iostream>
using namespace std;
class A {
public:
virtual A(){init();}
virtual ~A(){}
virtual void init(){
cout << "class A::init\n";
}
void test(){init();}
};
class B: public A{
public:
B():A(){}
virtual ~B(){}
virtual void init(){
cout << "class B::init\n";
}
};
int main(int,char**){
// only class A's init is called
B b;
// works as I expected
b.test();
return 0;
}
I have just encountered a problem when calling a virtual member function
in a constructor.
Only the memberfunction of the parent class is called instead of the
overloaded function.
I have attached a small example.
Is that bug or feature. If feature: is there an easy way to get the
behaviour as I am looking for?
Thanks
Karsten
#include <iostream>
using namespace std;
class A {
public:
virtual A(){init();}
virtual ~A(){}
virtual void init(){
cout << "class A::init\n";
}
void test(){init();}
};
class B: public A{
public:
B():A(){}
virtual ~B(){}
virtual void init(){
cout << "class B::init\n";
}
};
int main(int,char**){
// only class A's init is called
B b;
// works as I expected
b.test();
return 0;
}