same static variable in ALL template classes

Discussion in 'C++' started by Nils, Jan 6, 2004.

  1. Nils

    Nils Guest

    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.
    Nils, Jan 6, 2004
    #1
    1. Advertising

  2. Nils

    Jeff Schwab Guest

    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.
    >
    >
    >
    Jeff Schwab, Jan 6, 2004
    #2
    1. Advertising

  3. Nils

    Nils Guest

    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" <> schrieb im Newsbeitrag
    news:...
    > 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.
    > >
    > >
    > >

    >
    Nils, Jan 6, 2004
    #3
  4. Nils

    Jeff Schwab Guest

    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" <> schrieb im Newsbeitrag
    > news:...
    >
    >>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.
    >>>
    >>>
    >>>

    >>

    >
    >
    Jeff Schwab, Jan 6, 2004
    #4
  5. "Nils" <> 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
    Victor Bazarov, Jan 6, 2004
    #5
  6. Nils

    David Harmon Guest

    On Tue, 6 Jan 2004 22:30:11 +0100 in comp.lang.c++, "Nils"
    <> 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.
    David Harmon, Jan 7, 2004
    #6
  7. Nils

    Jeff Schwab Guest

    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" <> schrieb im Newsbeitrag
    >> news:...
    >>
    >>> 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.
    >>>>
    >>>>
    >>>>
    >>>

    >>
    >>

    >
    Jeff Schwab, Jan 7, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben
    Replies:
    3
    Views:
    411
  2. Ricardo Palomares Martinez
    Replies:
    2
    Views:
    364
    david.karr
    Apr 28, 2007
  3. Mug
    Replies:
    4
    Views:
    291
  4. Rit
    Replies:
    23
    Views:
    919
    Mike Schilling
    Jan 3, 2010
  5. Qi
    Replies:
    7
    Views:
    795
Loading...

Share This Page