473,659 Members | 2,965 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to make static Variables in Template classes

Hi,
Ich have a template class and want to initialize a static variable, that is
available for all objekts of the template class, doesn't matter what
template parameter is used.
Can someone please tell me how it will work!

Thanks a lot!
Nils
Jul 22 '05 #1
3 2397
"Nils" <sl***@web.de > wrote...
Ich have a template class and want to initialize a static variable, that is available for all objekts of the template class, doesn't matter what
template parameter is used.


You mean, like this:

template<class T> struct NilsClass {
static int someStaticVar;
};

// definition of the static member of the template
template<class T> int NilsClass<T>::s omeStaticVar = 42;

#include <iostream>
int main() {
NilsClass<doubl e> d;
std::cout << d.someStaticVar << std::endl;
NilsClass<std:: ostream> dd;
std::cout << dd.someStaticVa r << std::endl;
}

Victor
Jul 22 '05 #2
Yes, thanks a lot.
"Victor Bazarov" <v.********@com Acast.net> schrieb im Newsbeitrag
news:PT_Gb.2060 35$_M.911325@at tbi_s54...
"Nils" <sl***@web.de > wrote...
Ich have a template class and want to initialize a static variable, that

is
available for all objekts of the template class, doesn't matter what
template parameter is used.


You mean, like this:

template<class T> struct NilsClass {
static int someStaticVar;
};

// definition of the static member of the template
template<class T> int NilsClass<T>::s omeStaticVar = 42;

#include <iostream>
int main() {
NilsClass<doubl e> d;
std::cout << d.someStaticVar << std::endl;
NilsClass<std:: ostream> dd;
std::cout << dd.someStaticVa r << std::endl;
}

Victor

Jul 22 '05 #3

"Nils" <sl***@web.de > wrote in message
news:bs******** ***@ariadne.rz. tu-clausthal.de...
Yes, thanks a lot.
"Victor Bazarov" <v.********@com Acast.net> schrieb im Newsbeitrag
news:PT_Gb.2060 35$_M.911325@at tbi_s54...
"Nils" <sl***@web.de > wrote...
Ich have a template class and want to initialize a static variable,
that is
available for all objekts of the template class, doesn't matter what
template parameter is used.


You mean, like this:

template<class T> struct NilsClass {
static int someStaticVar;
};

// definition of the static member of the template
template<class T> int NilsClass<T>::s omeStaticVar = 42;

#include <iostream>
int main() {
NilsClass<doubl e> d;
std::cout << d.someStaticVar << std::endl;
NilsClass<std:: ostream> dd;
std::cout << dd.someStaticVa r << std::endl;
}

Victor



Nils / Victor - I think there may be a misunderstandin g - in the above
d.someStaticVar is not the same as dd.someStaticVa r

If you want the same var for all template instantiations as Nils originally
stated then you put the static in a non-template class and have the template
class derive from that.

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1626
by: Alibek | last post by:
Hello ! I have here template class with static members. and I put definition of the static member in the same ..h file where the class was declared. however, we are getting very stange errors, because static member occupy same place as some other non-static variables. and so application crashes. compiler somehow allocates same place for them.
4
512
by: Brad Kartchner | last post by:
I'm attempting to write a program with several child classes derived from a single parent class. The parent class has a couple of variables that need to be present in each of the child classes. However, I need the objects created from each of the derived classes to share these variables with other objects of the same class but not with other objects of the other derived classes. Let me try to clarify with an example:...
0
1632
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. Essentially at the point of the error, we invoke an internal interactive debugger that knows the classes within our system and allow us to walk around the objects that exist at the time of the fault. It mostly works fairly well. That catch being that we have to hand implement some of the code...
3
5701
by: Diebels | last post by:
Hi, I have some problems using static variables which results in a core dump. I have attached code and coredump to the end of my message. I am trying to implement a kind of factory design. I have a base class with several sub classes. In runtime I want to create a instance of a sub class and assign it to a base class pointer. Nothing fancy about that. I also want to be able in runtime to decide witch type of sub class that is to be...
1
9128
by: david.baird | last post by:
I have 3 classes, 1 which does not use templates, CGood, and 2 which do use templates, CBad, and COkay. I believe that all of them should work, but only CGood and COkay are working for me. CBad fails to compile. class CGood { public: typedef int arr_t; static const arr_t bar;
3
5845
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ section describes a solution using methods returning references to static objects. But consider:
2
2404
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual static function (which I know is illegal), but, is there a way to provide something similar? The class that is the target of my inquiry is a template class that interfaces to one of several derived classes through a pointer to a base class. The...
17
8367
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline the function if possible" (because if the compiler has the function definition available at an instantiation point, it will estimate whether to inline it or not, and do so if it estimates it would be beneficial, completely regardless of whether...
16
2172
by: Stefano Sabatini | last post by:
Hi all, I'm facing this design problem. I have a table which defines the behaviour of an Object in a given state according to the type of event it is receiving. So for each couple event,state I want I set in the table a different handler.
0
8428
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
8851
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...
0
8747
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6179
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
5649
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
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.