template pattern question

Discussion in 'C++' started by s, Jan 11, 2011.

  1. s

    s Guest

    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 Class1:public 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;
    }
    s, Jan 11, 2011
    #1
    1. Advertising

  2. s

    SG Guest

    On 11 Jan., 10:47, s <> 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
    SG, Jan 11, 2011
    #2
    1. Advertising

  3. s

    s Guest

    On Jan 11, 10:55 am, SG <> wrote:
    > On 11 Jan., 10:47, s <> 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!
    s, Jan 11, 2011
    #3
  4. 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).
    Juha Nieminen, Jan 11, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    17
    Views:
    1,862
    Chris Uppal
    Nov 16, 2005
  2. Chris Theis
    Replies:
    2
    Views:
    467
    Chris Theis
    Jul 24, 2003
  3. tom_usenet
    Replies:
    0
    Views:
    533
    tom_usenet
    Jul 24, 2003
  4. sunny
    Replies:
    1
    Views:
    461
    Salt_Peter
    Dec 7, 2006
  5. Pallav singh
    Replies:
    0
    Views:
    353
    Pallav singh
    Jan 22, 2012
Loading...

Share This Page