By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

template class static template type member address allocation

P: n/a
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

Sep 14 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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<intand MyClass<doubleare 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

Sep 14 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.