P
PengYu.UT
Hi,
A pure function is called in the base function constructor. It generate
a run time error: "pure virtual method called".
My problem is that class A have some derived classes. I want A's
constructor change its behaviour accounting to the derived class.
I tried to make A::fun() not pure virtual but virtual. It doesn't
generate any error. But A::fun() is called in A's construction, while I
want B::fun() be called. I just don't want to define a default virtual
function.
I'm wondering if there is any work around to solve this problem.
Best wishes,
Peng
#include <iostream>
class A{
public:
A(){ fun();}
virtual void fun() = 0;
};
class B : public A{
public:
virtual void fun(){
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
};
int main(int argc, char *argv[])
{
B b;
}
A pure function is called in the base function constructor. It generate
a run time error: "pure virtual method called".
My problem is that class A have some derived classes. I want A's
constructor change its behaviour accounting to the derived class.
I tried to make A::fun() not pure virtual but virtual. It doesn't
generate any error. But A::fun() is called in A's construction, while I
want B::fun() be called. I just don't want to define a default virtual
function.
I'm wondering if there is any work around to solve this problem.
Best wishes,
Peng
#include <iostream>
class A{
public:
A(){ fun();}
virtual void fun() = 0;
};
class B : public A{
public:
virtual void fun(){
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
};
int main(int argc, char *argv[])
{
B b;
}