same static variable in ALL template classes

N

Nils

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.
 
J

Jeff Schwab

Nils said:
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?
 
N

Nils

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?
 
J

Jeff Schwab

Nils said:
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';
}
 
V

Victor Bazarov

Nils said:
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;
 
D

David Harmon

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.
 
J

Jeff Schwab

Jeff said:
Is "vector" a type unrelated to the class template of the same name?


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';
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top