PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Method In Templated Super/Parent Called From Templated Derived/Child
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Method In Templated Super/Parent Called From Templated Derived/Child

Réponse
 
LinkBack Outils de la discussion
Vieux 17/10/2007, 12h05   #1
Rasmus Johansen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Method In Templated Super/Parent Called From Templated Derived/Child



"VirGin" <virgin69@gmail.com> wrote in message
news:1192643588.793533.122500@t8g2000prg.googlegro ups.com
> 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).

  Réponse avec citation
Vieux 17/10/2007, 18h53   #2
VirGin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Method In Templated Super/Parent Called From Templated Derived/Child

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-

  Réponse avec citation
Vieux 18/10/2007, 08h25   #3
Ulrich Eckhardt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Method In Templated Super/Parent Called From Templated Derived/Child

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 .

Uli

--
Sator Laser GmbH
Geschäftsführer: Ronald Boers, Amtsgericht Hamburg HR B62 932

  Réponse avec citation
Vieux 18/10/2007, 12h22   #4
VirGin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Method In Templated Super/Parent Called From Templated Derived/Child

Hola Rasmus,

On Oct 17, 3:05 am, "Rasmus Johansen" <rasmus_noert...@hotmail.com>
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-

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h40.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11752 seconds with 12 queries