473,394 Members | 1,852 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,394 software developers and data experts.

same static variable in ALL template classes

Hi,
I want to do a vector, that is the same in every template class, is it
possible?
Example:
....
class temp<int>.... static vector A;
class temp<float>... static vector A;

Now I want to get acces to A without using <...>.
Thank you.

Jul 22 '05 #1
6 1525
Nils wrote:
Hi,
I want to do a vector, that is the same in every template class, is it
possible?
Example:
...
class temp<int>.... static vector A;
class temp<float>... static vector A;
What C++ code is that supposed to represent?

Now I want to get acces to A without using <...>.
Thank you.


Jul 22 '05 #2
Ok, here real c++ code :=)

template <class T>
class test{
public:
static vector vec;
};
test<float> f;
test<int> i;

Now I want to get the vector:
f<float>::vec and i<int>::vec
to be the same, and also acces them without explicit using of the template
class, here float and int.
I know like it is above it is not possible. But is there a way to make it
possible?


"Jeff Schwab" <je******@comcast.net> schrieb im Newsbeitrag
news:5q********************@comcast.com...
Nils wrote:
Hi,
I want to do a vector, that is the same in every template class, is it
possible?
Example:
...
class temp<int>.... static vector A;
class temp<float>... static vector A;


What C++ code is that supposed to represent?

Now I want to get acces to A without using <...>.
Thank you.

Jul 22 '05 #3
Nils wrote:
Ok, here real c++ code :=)

template <class T>
class test{
public:
static vector vec;
Is "vector" a type unrelated to the class template of the same name?
};
test<float> f;
test<int> i;

Now I want to get the vector:
f<float>::vec and i<int>::vec
to be the same, and also acces them without explicit using of the template
class, here float and int.
I know like it is above it is not possible. But is there a way to make it
possible?

Somebody (not me) proposed a solution here recently that was along these
lines:

typedef char vector; // For all I know.

struct vec_base
{
vector vec;
};

template< typename T >
struct test: vec_base
{

};

int main( )
{
test<float> f;
test<int> i;

f.vec = 'f';
i.vec = 'i';
}

"Jeff Schwab" <je******@comcast.net> schrieb im Newsbeitrag
news:5q********************@comcast.com...
Nils wrote:
Hi,
I want to do a vector, that is the same in every template class, is it
possible?
Example:
...
class temp<int>.... static vector A;
class temp<float>... static vector A;


What C++ code is that supposed to represent?

Now I want to get acces to A without using <...>.
Thank you.



Jul 22 '05 #4
"Nils" <sl***@web.de> wrote...
Ok, here real c++ code :=)
"Real"?

template <class T>
class test{
public:
static vector vec;
'vector' of what? Or do you mean, say, a string?
};
test<float> f;
test<int> i;

Now I want to get the vector:
f<float>::vec and i<int>::vec
to be the same, and also acces them without explicit using of the template
class, here float and int.
I know like it is above it is not possible. But is there a way to make it
possible?


-------------------
template<class T>
class test {
public:
static string& commonstring; // note: a reference
};

extern string commonstring; // you will have to define it somewhere

template<class T> string& test<T>::commonstring = ::commonstring;
-------------------

Victor
Jul 22 '05 #5
On Tue, 6 Jan 2004 22:30:11 +0100 in comp.lang.c++, "Nils"
<sl***@web.de> was alleged to have written:
Hi,
I want to do a vector, that is the same in every template class, is it
possible?
Example:
...
class temp<int>.... static vector A;
class temp<float>... static vector A;


Every instantiation of a class template is a different class.
They don't share anything.

If you want them to share something, one possible answer is to derive
the template class(s) from a base class, and make the shared thing
static protected in the base class. It's not perfect.
Jul 22 '05 #6
Jeff Schwab wrote:
Nils wrote:
Ok, here real c++ code :=)

template <class T>
class test{
public:
static vector vec;

Is "vector" a type unrelated to the class template of the same name?
};
test<float> f;
test<int> i;

Now I want to get the vector:
f<float>::vec and i<int>::vec
to be the same, and also acces them without explicit using of the
template
class, here float and int.
I know like it is above it is not possible. But is there a way to make it
possible?


Somebody (not me) proposed a solution here recently that was along these
lines:

typedef char vector; // For all I know.

struct vec_base
{

static vector vec;
}; char vec_base::vec = '\0';
template< typename T >
struct test: vec_base
{

};

int main( )
{
test<float> f;
test<int> i;

f.vec = 'f';
i.vec = 'i';
}

"Jeff Schwab" <je******@comcast.net> schrieb im Newsbeitrag
news:5q********************@comcast.com...
Nils wrote:

Hi,
I want to do a vector, that is the same in every template class, is it
possible?
Example:
...
class temp<int>.... static vector A;
class temp<float>... static vector A;
What C++ code is that supposed to represent?
Now I want to get acces to A without using <...>.
Thank you.



Jul 22 '05 #7

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

Similar topics

3
by: Nils | last post by:
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...
4
by: Graham Dumpleton | last post by:
When you have a template class with a static member variable, ie., template<typename T> class handle { public: static void* id() { return &id_; } private: static int id_; };
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>...
16
by: Eric | last post by:
I have a static class member variable as follows: struct A { static void Set (int i) { v = i; } static int& Get () { return v; } static int v; }; int A::v; // define A::v in the cpp file
18
by: Erik Arner | last post by:
Hi, I really need some help here. After upgrading to g++ 3.4 I have run into all sorts of troubles that I'm sure depends on my lack of proper understanding of C++. I would now like to get it right...
5
by: Nils | last post by:
Hi. I ahve three classes A,B,C, where B and C are derived from A. A:>B,C. Furthermore each class has an static int variable with name id, static int id; Besides that the class A implements the...
8
by: Jon Slaughter | last post by:
Is there any way to declare a member of a base class to make the itself in the derived class to be static? i.e., I have two classes class Base { protected: char *name;
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...
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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
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...

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.