473,657 Members | 2,555 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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*pt r;
};

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 2285
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*pt r;
};
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<doublea re 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
2153
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 binary search tree. Note there is a requirement for this to be a Win32/MFC "friendly" class, thus the use of CObject and POSITION. There is also a requirement for there not to be a separate iterator class. template <class TYPE, class ARG_TYPE =...
7
2474
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> registrar; }; The constructor of Registrar does the registering when it is initialized.
3
4459
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 think this is really the issue. Here is a description of my problem. MathLibrary.dll: - SpaceTimeVector (uses various stuff from this dll)
10
2534
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. # include <iostream> # include <memory> class CallbackBase { public: virtual void operator()() const { };
7
2140
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 y = x; but I can't understand the last one which is Array<int>::element_type y = x; We have a typedef T element_type; in the template Array class see below
1
1116
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
1332
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 work OK as well, the problems appear in rather bulky code:( I'm trying to implement some signal processing algorithm using
2
2311
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 want to know how it works with the overloading of get<>(). boost::tupple<int,doubletup(1,2.0); double d=tup.get<2>(); // equal 2.0 // simple set up,
4
2570
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 TXpQAlloc<MyClass,N> N is a chunking factor (defaults to 10).
0
8403
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8316
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8833
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8509
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7345
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6174
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.