how to initialize static references to an object?

Discussion in 'C++' started by Zeppe, Apr 27, 2007.

  1. Zeppe

    Zeppe Guest

    Bram Kuijper wrote:
    > Hi all,
    >
    > as a C++ newbie, I got some question on the initialization of static
    > reference data members.
    >
    > Since it isn't possible to initialize static members of a class in the
    > constructor, I should initialize them in advance. However, the following
    > code, in which I first produce two classses and then try to assign a
    > reference of the first class to a static data member of the second class
    > doesn't work. It gives the following compiler error:
    >
    > error: no match for ‘operator=’ in ‘ga::ref = v’
    > note: candidates are: bla& bla::eek:perator=(const bla&)
    >
    > Since I don't wanna go into operator overloading for something this
    > simple, how should I properly initialize a static member referencing to
    > an object?
    >
    > cheers,
    > Bram
    >
    >
    > here is my code:
    >
    >
    > class bla
    > {
    > public:
    > bla();
    > ~bla();
    >
    > };
    >
    > class ga
    > {
    > public:
    > static bla &ref;


    pay attention with static member references, because of the
    initialization order. The static variables are initialized when the
    program is started, and the order of initialization is very important.

    > ga();
    > ~ga();
    >
    > };
    >


    >
    > int main()
    > {
    > bla v();
    > ga::ref = v;


    this is a quite serious error. ga::ref is created when you first run the
    program, before the main is executed. Then you are only assigning v to
    an unbinded reference.

    you should initialize ga::ref outside of any function, like

    bla& ga::ref = v;

    but v should be a static variable istantiated before in the same
    transactional unit, or the result of a function.

    If you want a shared object that you can change over time, you may want
    to use a pointer instead of a reference.

    Regards,

    Zeppe


    > return 0;
    > }
    >
     
    Zeppe, Apr 27, 2007
    #1
    1. Advertising

  2. Zeppe

    Bram Kuijper Guest

    Hi all,

    as a C++ newbie, I got some question on the initialization of static
    reference data members.

    Since it isn't possible to initialize static members of a class in the
    constructor, I should initialize them in advance. However, the following
    code, in which I first produce two classses and then try to assign a
    reference of the first class to a static data member of the second class
    doesn't work. It gives the following compiler error:

    error: no match for ‘operator=’ in ‘ga::ref = v’
    note: candidates are: bla& bla::eek:perator=(const bla&)

    Since I don't wanna go into operator overloading for something this
    simple, how should I properly initialize a static member referencing to
    an object?

    cheers,
    Bram


    here is my code:


    class bla
    {
    public:
    bla();
    ~bla();

    };

    class ga
    {
    public:
    static bla &ref;
    ga();
    ~ga();

    };



    int main()
    {
    bla v();
    ga::ref = v;
    return 0;
    }
     
    Bram Kuijper, Apr 27, 2007
    #2
    1. Advertising

  3. Zeppe

    SimpleCode Guest

    On 4ÔÂ28ÈÕ, ÉÏÎç12ʱ56·Ö, Bram Kuijper <> wrote:
    > Hi all,
    >
    > as a C++ newbie, I got some question on the initialization of static
    > reference data members.
    >
    > Since it isn't possible to initialize static members of a class in the
    > constructor, I should initialize them in advance. However, the following
    > code, in which I first produce two classses and then try to assign a
    > reference of the first class to a static data member of the second class
    > doesn't work. It gives the following compiler error:
    >
    > error: no match for 'operator=' in 'ga::ref = v'
    > note: candidates are: bla& bla::eek:perator=(const bla&)
    >
    > Since I don't wanna go into operator overloading for something this
    > simple, how should I properly initialize a static member referencing to
    > an object?
    >
    > cheers,
    > Bram
    >
    > here is my code:
    >
    > class bla
    > {
    > public:
    > bla();
    > ~bla();
    >
    > };
    >
    > class ga
    > {
    > public:
    > static bla &ref;
    > ga();
    > ~ga();
    >
    > };
    >
    > int main()
    > {
    > bla v();
    > ga::ref = v;
    > return 0;}


    class bla
    {
    public:
    bla();
    ~bla();



    };


    class ga
    {
    public:
    static bla &ref;
    ga();
    ~ga();


    };
    /////////////////////////////////add this line
    bla ga::&ref;

    int main()
    {
    bla v();
    ga::ref = v;
    return 0;

    }
     
    SimpleCode, Apr 27, 2007
    #3
  4. Zeppe

    Zeppe Guest

    SimpleCode wrote:

    > /////////////////////////////////add this line
    > bla ga::&ref;


    to add another error:

    ../testtraing.cpp:21: error: expected unqualified-id before ¡®&¡¯ token
    ../testtraing.cpp: In function ¡®int main()¡¯:
    ../testtraing.cpp:26: error: no match for ¡®operator=¡¯ in ¡®ga::ref = v¡¯
    ../testtraing.cpp:4: note: candidates are: bla& bla::eek:perator=(const bla&)

    fortunately id doesn't compile, because it would have been wrong, as I
    explained.

    Regards,

    Zeppe
     
    Zeppe, Apr 27, 2007
    #4
  5. Zeppe

    SimpleCode Guest

    On 4ÔÂ28ÈÕ, ÉÏÎç1ʱ34·Ö, Zeppe <>
    wrote:
    > SimpleCode wrote:
    > > /////////////////////////////////add this line
    > > bla ga::&ref;

    >
    > to add another error:
    >
    > ./testtraing.cpp:21: error: expected unqualified-id before '&' token
    > ./testtraing.cpp: In function 'int main()':
    > ./testtraing.cpp:26: error: no match for 'operator=' in 'ga::ref = v'
    > ./testtraing.cpp:4: note: candidates are: bla& bla::eek:perator=(const bla&)
    >
    > fortunately id doesn't compile, because it would have been wrong, as I
    > explained.
    >
    > Regards,
    >
    > Zeppe


    Thanks Zeppe.
    I got a serious mistake.It shoud "bla & ga::ref;"
    But it is another mistake.
    As you said,
    "
    this is a quite serious error. ga::ref is created when you first run
    the
    program, before the main is executed. Then you are only assigning v
    to
    an unbinded reference.
    but v should be a static variable istantiated before in the same
    transactional unit, or the result of a function.
    "
    Thank you.
     
    SimpleCode, Apr 27, 2007
    #5
    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. Seb
    Replies:
    2
    Views:
    4,723
  2. tropos
    Replies:
    3
    Views:
    483
  3. philwozza
    Replies:
    1
    Views:
    553
    Gianni Mariani
    May 14, 2006
  4. Bram Kuijper
    Replies:
    4
    Views:
    3,023
    Zeppe
    May 3, 2007
  5. Jack Bates
    Replies:
    3
    Views:
    326
    Steven D'Aprano
    Aug 6, 2011
Loading...

Share This Page