template class static template type member address allocation

Discussion in 'C++' started by Frederiek, Sep 14, 2006.

  1. Frederiek

    Frederiek Guest

    Hi,

    When modifying a data member in a class declaration, the static keyword
    specifies that one copy of the member is shared by all instances of the
    class.

    Does that mean that the address of the data member is the same for
    every instance of that class? I would expect so, but I noticed
    something that makes me doubt a little.

    What happens for example with the address of a static data member, that
    uses in fact a type as specified in the argument list of a template
    class?

    An example:

    template <class TYPE>
    class MyClass
    {
    static MyClass<TYPE> *ptr;
    };

    For every instance of this class
    - will there be only one address where the value of ptr is stored?
    - or will there be provided one ptr data member storage for each type
    of TYPE?
    - or does every instance, in this case, get its own static data member
    storage?

    Kind regards,

    Frederiek
    Frederiek, Sep 14, 2006
    #1
    1. Advertising

  2. Frederiek wrote:

    > When modifying a data member in a class declaration, the static keyword
    > specifies that one copy of the member is shared by all instances of the
    > class.


    Correct.

    > Does that mean that the address of the data member is the same for
    > every instance of that class?


    Yes, by definition.

    > I would expect so, but I noticed
    > something that makes me doubt a little.
    >
    > What happens for example with the address of a static data member, that
    > uses in fact a type as specified in the argument list of a template
    > class?
    >
    > An example:
    >
    > template <class TYPE>
    > class MyClass
    > {
    > static MyClass<TYPE> *ptr;
    > };


    The above is not a class, it's a class template. Until a class is
    instantiated, there is no class.

    > For every instance of this class
    > - will there be only one address where the value of ptr is stored?


    For a particular instantiation of the class template (e.g.,
    MyClass<int>), yes.

    > - or will there be provided one ptr data member storage for each type
    > of TYPE?


    Each instantiation for a different type is a different class, and so
    will have a different static member.

    In other words, MyClass<int> and MyClass<double> are NOT the same
    class. Accordingly, they do not share the same static member.

    > - or does every instance, in this case, get its own static data member
    > storage?


    No, every instance of a particular instantiation of the class template
    will share the same static member.

    By the way, you need to include a declaration of that static member
    somewhere as well.

    Best regards,

    Tom
    Thomas Tutone, Sep 14, 2006
    #2
    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. William Payne
    Replies:
    8
    Views:
    443
    Karthik Kumar
    Oct 11, 2004
  2. Brian Riis
    Replies:
    3
    Views:
    483
    Brian Riis
    Nov 2, 2004
  3. Siemel Naran
    Replies:
    4
    Views:
    780
    Micah Cowan
    Jan 12, 2005
  4. Ken
    Replies:
    24
    Views:
    3,817
    Ben Bacarisse
    Nov 30, 2006
  5. dolphin
    Replies:
    3
    Views:
    1,319
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page