How can i force my derivers to call a base function

Discussion in 'C++' started by ManicQin, Dec 19, 2007.

  1. ManicQin

    ManicQin Guest

    Hi lets say I have the next situation
    class base
    {
    pubic: ;)
    base() {}
    virtual ~base(){}
    virtual init()
    {//do init }
    };

    now i want that every deriver will call MY init function before he
    init his class...
    for some reasons I cannot put those calls in the cTor...

    one thing that I thought of is
    class base
    {
    public:
    init()
    {
    //do base's init
    secondPhaseInit();
    }
    protected:
    virtual secondPahseInit() = 0;
    }

    and then the deriver can only override secondPhaseInit and he needs to
    go through my init .
    But it's still not an idiot proof method... is there a way?
    ManicQin, Dec 19, 2007
    #1
    1. Advertising

  2. ManicQin

    Rahul Guest

    On Dec 19, 12:41 pm, ManicQin <> wrote:
    > Hi lets say I have the next situation
    > class base
    > {
    > pubic: ;)
    > base() {}
    > virtual ~base(){}
    > virtual init()
    > {//do init }
    >
    > };
    >
    > now i want that every deriver will call MY init function before he
    > init his class...
    > for some reasons I cannot put those calls in the cTor...
    >

    Could you specify the details of the reason?
    Derived class constructor can invoke any of the visible base class
    member functions, which is one of the reason for the base class
    constructor to be invoked before that of derived class constructor...
    Rahul, Dec 19, 2007
    #2
    1. Advertising

  3. * ManicQin:
    > Hi lets say I have the next situation
    > class base
    > {
    > pubic: ;)
    > base() {}
    > virtual ~base(){}
    > virtual init()
    > {//do init }
    > };
    >
    > now i want that every deriver will call MY init function before he
    > init his class...
    > for some reasons I cannot put those calls in the cTor...
    >
    > one thing that I thought of is
    > class base
    > {
    > public:
    > init()
    > {
    > //do base's init
    > secondPhaseInit();
    > }
    > protected:
    > virtual secondPahseInit() = 0;
    > }
    >
    > and then the deriver can only override secondPhaseInit and he needs to
    > go through my init .
    > But it's still not an idiot proof method... is there a way?


    Your question is extremely unclear.

    If it's just about how to achieve two-phase construction, the best is
    probably to read up on class invariants in order to understand why that
    is a generally a Bad Idea.

    Otherwise, have a look at the FAQ, e.g. <url:
    http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.6>.


    Cheers, & hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Dec 19, 2007
    #3
  4. ManicQin

    ManicQin Guest

    On Dec 19, 10:12 am, Rahul <> wrote:

    > Could you specify the details of the reason?
    > Derived class constructor can invoke any of the visible base class
    > member functions, which is one of the reason for the base class
    > constructor to be invoked before that of derived class constructor...


    Thank you rahul on that great introduction to c++ inheritence.
    And thank you Alf.

    I probably misled you both because i called my function init.
    The question is not about cTor or initialization in particularly.

    take the next snip

    class base
    {
    public:

    foo()
    { /do something - any thing!
    goo();
    }
    protected:
    virtual goo() = 0;
    }

    class derive : public base
    {
    protected:
    goo()
    {
    //do something else...
    }

    }

    int main()
    {
    derive a;

    a.foo(); //first base.foo() will execute and then derive.goo()
    return 0;
    }

    the problem is that this pattern (I think they call it Template
    Method) is not IDIOT PROOF.
    tomorrow a coder would come and change my code so he would'nt call my
    base function. (infact it happened today in spite of all the comments
    i left!) I'm just looking for a different way to implement this.

    thank you
    ManicQin, Dec 19, 2007
    #4
  5. ManicQin

    James Kanze Guest

    On Dec 19, 10:28 am, ManicQin <> wrote:
    > On Dec 19, 10:12 am, Rahul <> wrote:


    > I probably misled you both because i called my function init.
    > The question is not about cTor or initialization in
    > particularly.


    > class base
    > {
    > public:
    > foo()
    > { /do something - any thing!
    > goo();
    > }
    > protected:


    Why protected, and not private?

    > virtual goo() = 0;
    > }


    > class derive : public base
    > {
    > protected:
    > goo()
    > {
    > //do something else...
    > }
    > }


    > int main()
    > {
    > derive a;


    > a.foo(); //first base.foo() will execute and then derive.goo()
    > return 0;
    > }


    > the problem is that this pattern (I think they call it Template
    > Method) is not IDIOT PROOF.
    > tomorrow a coder would come and change my code so he would'nt
    > call my base function. (infact it happened today in spite of
    > all the comments i left!) I'm just looking for a different way
    > to implement this.


    I'm not sure I understand. Which code are you talking about?
    If client code doesn't call some particular function, there's
    not much you can do about it. For that matter, he may not even
    create an instance of your class. And the author of the derived
    class shouldn't call your base class function; that would result
    in endless recursion. Could you please give an exact example of
    the type of error you're trying to avoid.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Dec 19, 2007
    #5
  6. ManicQin wrote:
    > class base
    > {
    > public:
    >
    > foo()
    > { /do something - any thing!
    > goo();
    > }
    > protected:
    > virtual goo() = 0;
    > }
    >
    > class derive : public base
    > {
    > protected:
    > goo()
    > {
    > //do something else...
    > }
    >
    > }
    >
    > int main()
    > {
    > derive a;
    >
    > a.foo(); //first base.foo() will execute and then derive.goo()
    > return 0;
    > }
    >
    > the problem is that this pattern (I think they call it Template
    > Method) is not IDIOT PROOF.
    > tomorrow a coder would come and change my code so he would'nt call my
    > base function. (infact it happened today in spite of all the comments
    > i left!) I'm just looking for a different way to implement this.


    This is still too little information for us, we cannot assess whether the
    overall design is flawed. Why is it necessary that the user calls your base
    class method? Maybe your class derive should not even be derived from base if
    you have such strange dependencies in your code.

    Regards,
    Stuart
    Stuart Redmann, Dec 19, 2007
    #6
  7. ManicQin

    ManicQin Guest

    James Kanze:
    > Why protected, and not private?

    That was a typo, sorry.


    Stuart Redmann :
    > This is still too little information for us, we cannot assess whether the
    > overall design is flawed. Why is it necessary that the user calls your base
    > class method? Maybe your class derive should not even be derived from base if
    > you have such strange dependencies in your code.


    My question was'nt about the design.
    I think that if after 4 threads I didnt manage to explain my question,
    then maybe it is better to just leave it be.

    thank you for your help.
    ManicQin, Dec 19, 2007
    #7
  8. ManicQin

    Pete Becker Guest

    On 2007-12-19 07:25:01 -0500, ManicQin <> said:

    >
    > My question was'nt about the design.
    >


    The question wasn't, but the answer may well be. Awkward coding
    problems often can be resolved by changing the design. Especially when
    the immediate answer to the awkward coding problem is, as here, "you
    can't do that."

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)
    Pete Becker, Dec 19, 2007
    #8
  9. ManicQin

    red floyd Guest

    ManicQin wrote:
    > Hi lets say I have the next situation
    > class base
    > {
    > pubic:

    Yeah, I know it's a typo, but you have to admit, this is a *GREAT* way
    to define your private parts :)
    > base() {}
    > virtual ~base(){}
    > virtual init()
    > {//do init }
    > };
    >
    red floyd, Dec 19, 2007
    #9
  10. ManicQin

    Andre Kostur Guest

    ManicQin <> wrote in news:d4346905-60c3-4237-bb06-
    :

    > On Dec 19, 10:12 am, Rahul <> wrote:
    >
    >> Could you specify the details of the reason?
    >> Derived class constructor can invoke any of the visible base class
    >> member functions, which is one of the reason for the base class
    >> constructor to be invoked before that of derived class constructor...

    >
    > Thank you rahul on that great introduction to c++ inheritence.
    > And thank you Alf.
    >
    > I probably misled you both because i called my function init.
    > The question is not about cTor or initialization in particularly.
    >
    > take the next snip
    >
    > class base
    > {
    > public:
    >
    > foo()
    > { /do something - any thing!
    > goo();
    > }
    > protected:
    > virtual goo() = 0;
    > }
    >
    > class derive : public base
    > {
    > protected:
    > goo()
    > {
    > //do something else...
    > }
    >
    > }
    >
    > int main()
    > {
    > derive a;
    >
    > a.foo(); //first base.foo() will execute and then derive.goo()
    > return 0;
    > }
    >
    > the problem is that this pattern (I think they call it Template
    > Method) is not IDIOT PROOF.
    > tomorrow a coder would come and change my code so he would'nt call my
    > base function. (infact it happened today in spite of all the comments
    > i left!) I'm just looking for a different way to implement this.


    You cannot prevent malice. Severly discipline the other developer for
    breaking the design without consulting the appropriate stakeholders.
    Andre Kostur, Dec 19, 2007
    #10
    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. Yu Lianqing
    Replies:
    1
    Views:
    379
    John Harrison
    Jul 31, 2003
  2. tomek
    Replies:
    2
    Views:
    676
    Andrey Tarasevich
    Dec 1, 2003
  3. tomek
    Replies:
    3
    Views:
    745
    David Fisher
    Nov 30, 2003
  4. George2
    Replies:
    0
    Views:
    351
    George2
    Mar 17, 2008
  5. Albright
    Replies:
    3
    Views:
    446
    Alf P. Steinbach
    Feb 17, 2009
Loading...

Share This Page