pointer to a template class

Discussion in 'C++' started by Gert Van den Eynde, Jun 3, 2004.

  1. Hi all,

    A beginners question....

    I've got a template class

    template <class T> classA {...}

    In an other class, I want to pass a pointer to an instance of classA as a
    function argument. How do I do this? Also, I want to have a data member for
    this pointer. How do I declare this datamember?


    thanks,
    gert
    Gert Van den Eynde, Jun 3, 2004
    #1
    1. Advertising

  2. Gert Van den Eynde wrote:

    > A beginners question....
    >
    > I've got a template class


    Nope.

    >
    > template <class T> classA {...}


    You have a _class_template_. That's not the same thing.

    > In an other class, I want to pass a pointer to an instance of classA


    There is no such thing as "instance of classA" (no matter how
    many times you use the word 'class' in its name, it's still just
    a _template_). In order to have an _instance_ you need to make
    an _instantiation_ of the 'classA' _template_. To do that you
    need to give it a valid _template_argument_.

    > as a
    > function argument. How do I do this? Also, I want to have a data member for
    > this pointer. How do I declare this datamember?


    Again, there is no such thing as "a pointer to a template". You
    can only have a pointer to an object of a _particular_type_. In
    order to _become_ a particular type, a template needs to be
    _instantiated_, and that means you need to give it the set of
    valid arguments (in your case, one argument that is in turn also
    a type).

    Example:

    classA<int> an_object_of_classA_of_int;
    classA<double>* a_pointer_to_an_object_of_classA_of_double;

    Victor
    Victor Bazarov, Jun 3, 2004
    #2
    1. Advertising

  3. Hi,

    > I've got a template class
    >
    > template <class T> classA {...}
    >
    > In an other class, I want to pass a pointer to an instance of classA as a
    > function argument. How do I do this? Also, I want to have a data member

    for
    > this pointer. How do I declare this datamember?


    Instance of classA is a class (because classA is a template), so you cannot
    pass it to some function or store it in a variable. Classes cannot be stored
    or passed to functions. You can only do that with classA<X> where X is some
    type name. Instance of classA<X> is an object, and you can store or pass it
    freely.

    Alternatively, you could define the function you want to pass an instance of
    classA as a template function:

    template<typename T> void SomeFunction(classA<T> p);

    Compiler will then automatically create separate version of SomeFunction for
    every type T you use it with. You cannot do that with data member though,
    because there are no data members templates in C++.

    Marcin
    Marcin Kalicinski, Jun 3, 2004
    #3
  4. "Marcin Kalicinski" <> wrote in message news:<c9ncqc$bme$>...
    > Hi,
    >
    > > I've got a template class
    > >
    > > template <class T> classA {...}
    > >
    > > In an other class, I want to pass a pointer to an instance of classA as a
    > > function argument. How do I do this? Also, I want to have a data member

    > for
    > > this pointer. How do I declare this datamember?

    >
    > Instance of classA is a class (because classA is a template), so you cannot
    > pass it to some function or store it in a variable. Classes cannot be stored
    > or passed to functions. You can only do that with classA<X> where X is some
    > type name. Instance of classA<X> is an object, and you can store or pass it
    > freely.


    Actually, you can pass a class to a function _template_, so you could
    pass a class template instance as well:

    template< class C > void foo();
    template< class T > classA {};
    foo< classA<int> > (); // passes classA<int> to foo< > ( )

    > Alternatively, you could define the function you want to pass an instance of
    > classA as a template function:
    >
    > template<typename T> void SomeFunction(classA<T> p);
    >
    > Compiler will then automatically create separate version of SomeFunction for
    > every type T you use it with.


    True, if you have instances of instances of classA :), e.g an instance of
    classA<int>, you can pass that and the compiler will generate
    SomeFunction<int> to handle the classA<int>. But sometimes you can't do
    that, because you don't have the object yet. In such cases, you pass
    the class and get an instance of class:

    template<typename T>
    classA<T> create() { return classA<T>( ); }

    create<void>() creates a temporary classA<void>.

    > You cannot do that with data member though,
    > because there are no data members templates in C++.


    I'm not sure what that means. C++ has only class templates and
    function templates, but class templates certainly can have
    data members whose types are dictated by the surrounding template.

    template< typename DATA >
    class linked_list_node {
    DATA d; // <=== data member
    linked_list_node* next, prev;
    ...
    };
    Michiel Salters, Jun 4, 2004
    #4
  5. Michiel Salters wrote:
    > "Marcin Kalicinski" <> wrote in message news:<c9ncqc$bme$>...
    >
    >>Hi,
    >>
    >>
    >>>I've got a template class
    >>>
    >>>template <class T> classA {...}
    >>>
    >>>In an other class, I want to pass a pointer to an instance of classA as a
    >>>function argument. How do I do this? Also, I want to have a data member

    >>
    >> for
    >>
    >>>this pointer. How do I declare this datamember?

    >>
    >>Instance of classA is a class (because classA is a template), so you cannot
    >>pass it to some function or store it in a variable. Classes cannot be stored
    >>or passed to functions. You can only do that with classA<X> where X is some
    >>type name. Instance of classA<X> is an object, and you can store or pass it
    >>freely.

    >
    >
    > Actually, you can pass a class to a function _template_, so you could
    > pass a class template instance as well:
    >
    > template< class C > void foo();
    > template< class T > classA {};
    > foo< classA<int> > (); // passes classA<int> to foo< > ( )


    You're not passing it to a function. You're giving it to the compiler
    to use as a template argument. That's not the same thing.

    > [...]
    >
    >>You cannot do that with data member though,
    >>because there are no data members templates in C++.

    >
    >
    > I'm not sure what that means. C++ has only class templates and
    > function templates, but class templates certainly can have
    > data members whose types are dictated by the surrounding template.
    >
    > template< typename DATA >
    > class linked_list_node {
    > DATA d; // <=== data member
    > linked_list_node* next, prev;
    > ...
    > };


    He meant _independent_ member templates that are data members:

    template<typename T>
    class whatever {
    template<class S> double A; // what's that going to do?
    };

    You can have member templates for functions:

    template<typename T>
    class whatever {
    template<class S> double foo(); // 'foo' is a func template
    };

    Victor
    Victor Bazarov, Jun 4, 2004
    #5
    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. christopher diggins
    Replies:
    16
    Views:
    744
    Pete Becker
    May 4, 2005
  2. Vijai Kalyan
    Replies:
    4
    Views:
    701
    Vijai Kalyan
    Nov 8, 2005
  3. Replies:
    1
    Views:
    2,100
    Gianni Mariani
    Jun 8, 2007
  4. Replies:
    7
    Views:
    445
    Adam Nielsen
    Sep 28, 2007
  5. A L
    Replies:
    1
    Views:
    509
    Alf P. Steinbach /Usenet
    Aug 25, 2010
Loading...

Share This Page