|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 (permalink) |
|
Messages: n/a
Hébergeur: |
"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). |
|
|
|
#2 (permalink) |
|
Messages: n/a
Hébergeur: |
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- |
|
|
|
#3 (permalink) |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 (permalink) |
|
Messages: n/a
Hébergeur: |
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- |
|
![]() |
| Outils de la discussion | |
|
|