Method In Templated Super/Parent Called From Templated Derived/Child

Discussion in 'C++' started by Rasmus Johansen, Oct 17, 2007.

  1. "VirGin" <> wrote in message
    news:
    > Using the above format, I want to call TheParentClass::TheMethod from
    > within TheKid::TheMethod.


    TheParentClass is the class template (i.e. not actually a class, but a
    template for one), and as you hinted at earlier you need to use the template
    class. So you should use:
    TheParentClass<TheKid>::TheMethod();


    > BTW, the way I did it the first time generated a 'requires template
    > argument list' error


    This is because to call a method you need to call it on a template class,
    not a class template and to go from a class template to a template class you
    need to provide an argument list (<TheKid> in this case).
    Rasmus Johansen, Oct 17, 2007
    #1
    1. Advertising

  2. Rasmus Johansen

    VirGin Guest

    Hola Folks,

    I have a class template (or template class, depending on how one was
    taught). I have a class that is derived from the template class. I
    have a method in the parent that is overloaded in the child. If the
    child is unable to process the data being operated on, I want it to
    either return control to the parent or call the parent's version of
    the method. Since templates are involved, I'm unsure of the best way
    of doing this. What are some of the ways of doing this?

    Wow, that isn't clear at all.


    Parent Class:
    template <typename DrvdCls> class TheParentClass
    {
    void TheMethod(void);
    ...
    template <typename DrvdCls> inline void
    TheParentClass<DrvdCls>::TheMethod(void)
    {
    blah.. blah
    }
    }

    Derived Class:
    class TheKid:public TheParentClass<TheKid>
    {
    void TheMethod(void);
    }

    Using the above format, I want to call TheParentClass::TheMethod from
    within TheKid::TheMethod.

    Does that make sense? If so, what are different ways I could
    accomplish this.

    BTW, the way I did it the first time generated a 'requires template
    argument list' error

    anyway, thanx in advance


    -V-
    VirGin, Oct 17, 2007
    #2
    1. Advertising

  3. Note: f'up to clc++, there is nothing win32-related here.

    VirGin wrote:
    > I have a class template (or template class, depending on how one was
    > taught). I have a class that is derived from the template class. I
    > have a method in the parent that is overloaded in the child. If the
    > child is unable to process the data being operated on, I want it to
    > either return control to the parent or call the parent's version of
    > the method. Since templates are involved, I'm unsure of the best way
    > of doing this. What are some of the ways of doing this?
    >
    > Wow, that isn't clear at all.
    >
    >
    > Parent Class:
    > template <typename DrvdCls> class TheParentClass
    > {
    > void TheMethod(void);
    > ...
    > template <typename DrvdCls> inline void
    > TheParentClass<DrvdCls>::TheMethod(void)
    > {
    > blah.. blah
    > }
    > }


    This code is broken:
    1. Missing semicolon at the end. This might seem irrelevant, but it shows
    that this is not the code you tried to compile.
    2. The syntax for definition of TheMethod inside the class is wrong.
    3. Not an error, but an empty parameter list in C++ means no parameters. The
    void is not necessary.

    > Derived Class:
    > class TheKid:public TheParentClass<TheKid>
    > {
    > void TheMethod(void);
    > }


    4. Derivation means ISA relationship. Your code says that TheKid is a
    TheParentClass. I'd simply drop the term parent/child and substitute them
    for base/derived. This is not a C++ error though.

    > Using the above format, I want to call TheParentClass::TheMethod from
    > within TheKid::TheMethod.
    >
    > Does that make sense? If so, what are different ways I could
    > accomplish this.


    Well, then do it! However, for name resolution in template classes, the
    baseclass is not(!) considered normally. There are two ways around this:

    // explicitly invoke baseclass
    TheParentClass<TheKid>::TheMethod();
    // trigger normal name lookup
    this->TheMethod();


    > BTW, the way I did it the first time generated a 'requires template
    > argument list' error


    Well, that is another point towards #1 above. Show the real code. Show the
    real error message. Without real information it's hard to help.

    Uli

    --
    Sator Laser GmbH
    Geschäftsführer: Ronald Boers, Amtsgericht Hamburg HR B62 932
    Ulrich Eckhardt, Oct 18, 2007
    #3
  4. Rasmus Johansen

    VirGin Guest

    Hola Rasmus,

    On Oct 17, 3:05 am, "Rasmus Johansen" <>
    wrote:
    > > > Using the above format, I want to call TheParentClass::TheMethod from
    > > > within TheKid::TheMethod.


    > > TheParentClass is the class template (i.e. not actually a class, but a
    > > template for one), and as you hinted at earlier you need to use the template
    > > class. So you should use:
    > > TheParentClass<TheKid>::TheMethod();


    > > > BTW, the way I did it the first time generated a 'requires template
    > > > argument list' error


    > > This is because to call a method you need to call it on a template class,
    > > not a class template and to go from a class template to a template class you
    > > need to provide an argument list (<TheKid> in this case).


    Ouch!

    That was the second thing I did. Unfortunately it failed. However, it
    failed because of Programmer Error. It turned out that I did not type
    the name of the child class correctly when I added:
    TheParentClass<TheKid>::TheMethod() to TheMethod.

    The error that I received was about non-static member functions. I
    guess I was too tired to realize what was happening.

    Adding TheParentClass<TheKid>::TheMethod() to TheKid::TheMethod()
    works.

    Thank You!


    -V-
    VirGin, Oct 18, 2007
    #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. Guest

    super.super.super how?

    Guest, Feb 19, 2005, in forum: Java
    Replies:
    24
    Views:
    10,742
    Darryl Pierce
    Feb 24, 2005
  2. Replies:
    1
    Views:
    384
    myork
    May 23, 2007
  3. Replies:
    1
    Views:
    372
    Victor Bazarov
    May 23, 2007
  4. Noel Dolan
    Replies:
    0
    Views:
    216
    Noel Dolan
    Jul 18, 2004
  5. Bitswapper
    Replies:
    5
    Views:
    119
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page