about template(newbie)

Discussion in 'C++' started by sam, Jan 22, 2007.

  1. sam

    sam Guest

    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 ?
     
    sam, Jan 22, 2007
    #1
    1. Advertising

  2. sam

    Mike Wahler Guest

    "sam" <samsoft206@yahoo.com> wrote in message
    news:1169479308.770164.9000@51g2000cwl.googlegroups.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
     
    Mike Wahler, Jan 22, 2007
    #2
    1. Advertising

  3. sam

    Jim Langston Guest

    "sam" <samsoft206@yahoo.com> wrote in message
    news:1169479308.770164.9000@51g2000cwl.googlegroups.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).
     
    Jim Langston, Jan 22, 2007
    #3
  4. Jim Langston wrote:
    > "sam" <samsoft206@yahoo.com> 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
     
    Thomas Tutone, Jan 22, 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. Chris Theis
    Replies:
    2
    Views:
    487
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    552
    tom_usenet
    Jul 24, 2003
  3. gary.bernstein@gmail.com
    Replies:
    1
    Views:
    2,129
    Gianni Mariani
    Jun 8, 2007
  4. Peng Yu
    Replies:
    3
    Views:
    793
    Thomas J. Gritzan
    Oct 26, 2008
  5. nguillot
    Replies:
    5
    Views:
    540
Loading...

Share This Page