template class static template type member address allocation

F

Frederiek

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
 
T

Thomas Tutone

Frederiek said:
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 said:
- 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.

- 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
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top