473,406 Members | 2,816 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,406 software developers and data experts.

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 2378
"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>::someStaticVar = 42;

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

Victor
Jul 22 '05 #2
Yes, thanks a lot.
"Victor Bazarov" <v.********@comAcast.net> schrieb im Newsbeitrag
news:PT_Gb.206035$_M.911325@attbi_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>::someStaticVar = 42;

#include <iostream>
int main() {
NilsClass<double> d;
std::cout << d.someStaticVar << std::endl;
NilsClass<std::ostream> dd;
std::cout << dd.someStaticVar << 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.********@comAcast.net> schrieb im Newsbeitrag
news:PT_Gb.206035$_M.911325@attbi_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>::someStaticVar = 42;

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

Victor



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

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
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...
4
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. ...
0
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...
3
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...
1
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...
3
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...
2
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...
17
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...
16
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
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...
0
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...

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.