S
s
I want to make a baseclass with a constructor and destructor and have
these call methods in the derived classes which vary. I though the
'template' pattern looked appropriate and tried the following, which
doesn't compile and I'm not sure why. The error message is self-
explanatory:
abstract virtual 'virtual void IClass::init()' called from constructor
but clearly I have then misunderstood the template pattern
implementation; what would be correct? why doesnt the baseclass
constructor call to init() delegate to the derived class?
#include <iostream>
class IClass{
public:
IClass(){
init();
}
protected:
virtual void init()=0;
};
class Class1ublic IClass{
public:
Class1(){
//nop
}
virtual void
init(){
std::cout<<"template method class 1"<<std::endl;
}
};
int main (int argc, char * const argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
IClass * ic=new Class1();
return 0;
}
these call methods in the derived classes which vary. I though the
'template' pattern looked appropriate and tried the following, which
doesn't compile and I'm not sure why. The error message is self-
explanatory:
abstract virtual 'virtual void IClass::init()' called from constructor
but clearly I have then misunderstood the template pattern
implementation; what would be correct? why doesnt the baseclass
constructor call to init() delegate to the derived class?
#include <iostream>
class IClass{
public:
IClass(){
init();
}
protected:
virtual void init()=0;
};
class Class1ublic IClass{
public:
Class1(){
//nop
}
virtual void
init(){
std::cout<<"template method class 1"<<std::endl;
}
};
int main (int argc, char * const argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
IClass * ic=new Class1();
return 0;
}