473,320 Members | 2,000 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

template class static template type member address allocation

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
1 2249
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Nobody | last post by:
This is sort of my first attempt at writing a template container class, just wanted some feedback if everything looks kosher or if there can be any improvements. This is a template class for a...
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
3
by: David Komanek | last post by:
Hi all, I am trying to learn more about how to use g++/Cygwin to produce dll files on WinXP. And I have a problem which at the first look seems to be an obvious dll-export problem, but I don't...
10
by: ma740988 | last post by:
Part of my confusion here is certainly my ignorance of templates and std::auto_ptr. Two topics, I've perused but need to really _delve_ into. In any event, consider the 'test' source. #...
7
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
1
by: Vladimir Nesterovsky | last post by:
Hello, I have a code which behaves differently if template parameter has a specified member: #include <iostream> template<class T, bool (T::*)()> class member_bind {
3
by: Serge Skorokhodov (216716244) | last post by:
Hi, I just seeking advice. Some background information first because I've run into issues that seems pretty obscure to me:( Quick search through KB yields next to nothing. Simplified samples...
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
4
by: AndrewD | last post by:
Hey C++ folks, I created this today, just for fun. You can make object allocation for any class around 6 times faster, simply by doing the following. class MyClass : public...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.