Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > about template(newbie)

Reply
Thread Tools

about template(newbie)

 
 
sam
Guest
Posts: n/a
 
      01-22-2007
Hi,
I want to ask question about following code:
template <class T>
class Wrapper {
public:
T* operator->() { return &myT; }
private:
T myT;
};

int main() {
Wrapper<Thing> wThing;
wThing->Foo(); // calls Thing::Foo()
...
}
Whats the meaning of Thing here ?

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      01-22-2007

"sam" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) s.com...
> Hi,
> I want to ask question about following code:
> template <class T>
> class Wrapper {
> public:
> T* operator->() { return &myT; }
> private:
> T myT;
> };
>
> int main() {
> Wrapper<Thing> wThing;
> wThing->Foo(); // calls Thing::Foo()
> ...
> }
> Whats the meaning of Thing here ?


'Thing' (whose definition you did not show) is
a type, which is being used as the template
argument for the class template 'Wrapper'.
I.e., when the template is instantiated, 'Thing'
takes the place of 'T'.

-Mike


 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      01-22-2007
"sam" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) s.com...
> Hi,
> I want to ask question about following code:
> template <class T>
> class Wrapper {
> public:
> T* operator->() { return &myT; }
> private:
> T myT;
> };
>
> int main() {
> Wrapper<Thing> wThing;
> wThing->Foo(); // calls Thing::Foo()
> ...
> }
> Whats the meaning of Thing here ?


int, float, bool, double, someclass, someotherclass, etc... Just about any
type.
I mean,
Wrapper<int> wThing;
Wrapper<double> wThing;
Wrapper<unsigned char> wThing;
Wrapper<MyClasss> wThing;

are all valid (as long as MyClass is a valid class in the last example).


 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      01-22-2007

Jim Langston wrote:
> "sam" <(E-Mail Removed)> wrote in message


> > I want to ask question about following code:
> > template <class T>
> > class Wrapper {
> > public:
> > T* operator->() { return &myT; }
> > private:
> > T myT;
> > };
> >
> > int main() {
> > Wrapper<Thing> wThing;
> > wThing->Foo(); // calls Thing::Foo()
> > ...
> > }
> > Whats the meaning of Thing here ?

>
> int, float, bool, double, someclass, someotherclass, etc... Just about any
> type.
> I mean,
> Wrapper<int> wThing;
> Wrapper<double> wThing;
> Wrapper<unsigned char> wThing;
> Wrapper<MyClasss> wThing;
>
> are all valid (as long as MyClass is a valid class in the last example).


None of the POD types you listed above have a Foo() member function, so
Thing presumably isn't any of those.

Best regards,

Tom

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments