Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template pattern question

Reply
Thread Tools

template pattern question

 
 
s
Guest
Posts: n/a
 
      01-11-2011
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;
}
 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      01-11-2011
On 11 Jan., 10:47, s <shaun....@cern.ch> wrote:
> I want to make a baseclass with a constructor and destructor and have
> these call methods in the derived classes which vary.


Why?

> 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?


See C++ FAQ 23.5

Cheers!
SG
 
Reply With Quote
 
 
 
 
s
Guest
Posts: n/a
 
      01-11-2011
On Jan 11, 10:55*am, SG <s.gesem...@gmail.com> wrote:
> On 11 Jan., 10:47, s <shaun....@cern.ch> wrote:
>
> > I want to make a baseclass with a constructor and destructor and have
> > these call methods in the derived classes which vary.

>
> Why?
>
> > 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?

>
> See C++ FAQ 23.5
>
> Cheers!
> SG


That's a pretty complete explanation, thanks a lot!
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      01-11-2011
s <> wrote:
> why doesnt the baseclass
> constructor call to init() delegate to the derived class?


You can't call a derived class function from the constructor of the
base class because at that point the derived part has yet not been
constructed (effectively it "doesn't exist" at that point).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57