about template(newbie)

S

sam

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 ?
 
M

Mike Wahler

sam said:
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
 
J

Jim Langston

sam said:
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).
 
T

Thomas Tutone

Jim said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top