accessing static member variables

Discussion in 'C++' started by sowmiya.ts@gmail.com, Apr 4, 2008.

  1. Guest

    Hi,

    I have a header file cl.h which has a static variable queue.

    //cl.h
    class Cl
    {
    private:
    static map<int,std::string> Queue_;
    public:
    std::string receive(int type_of_receive,int pid_r);
    ....
    };

    //cl.cpp
    map<int,std::string> Cl::Queue_;
    std::string receive(int type_of_receive,int pid_r)
    {
    //Queue_ is used here
    }

    //rrm.h
    class Rrm
    {
    private:
    Cl cl_ob_;
    ....
    };

    //rrm.cpp uses cl_ob_

    //sl.h
    class Sl
    {
    private:
    Cl cl_ob_;
    ....
    };
    //sl.cpp uses cl_ob_

    main function is in some other file which uses rrm and sl header
    files.

    Problem: sl and rrm have different values for Queue_ .

    Help me out please.

    Thanks,
    Sowmiya
     
    , Apr 4, 2008
    #1
    1. Advertising

  2. Lionel B Guest

    On Fri, 04 Apr 2008 04:21:10 -0700, sowmiya.ts wrote:

    > Hi,
    >
    > I have a header file cl.h which has a static variable queue.
    >
    > //cl.h
    > class Cl
    > {
    > private:
    > static map<int,std::string> Queue_;
    > public:
    > std::string receive(int type_of_receive,int pid_r);
    > ....
    > };
    >
    > //cl.cpp
    > map<int,std::string> Cl::Queue_;
    > std::string receive(int type_of_receive,int pid_r) {
    > //Queue_ is used here
    > }
    >
    > //rrm.h
    > class Rrm
    > {
    > private:
    > Cl cl_ob_;
    > ....
    > };
    >
    > //rrm.cpp uses cl_ob_
    >
    > //sl.h
    > class Sl
    > {
    > private:
    > Cl cl_ob_;
    > ....
    > };
    > //sl.cpp uses cl_ob_
    >
    > main function is in some other file which uses rrm and sl header files.
    >
    > Problem: sl and rrm have different values for Queue_ .


    I'm not clear what you mean... there will be only one Cl::Queue_ object;
    do you mean that it appears to contain different data when accessed from
    different points in your program? But you are presumably modifying its
    contents somewhere, so what do you get and what did you expect to get?

    More information required - a small *compilable* program that
    demonstrates your problem is always a good idea.

    --
    Lionel B
     
    Lionel B, Apr 4, 2008
    #2
    1. Advertising

  3. On Fri, 04 Apr 2008 04:21:10 -0700, sowmiya.ts wrote:

    > Hi,
    >
    > I have a header file cl.h which has a static variable queue.
    >
    > //cl.h
    > class Cl
    > {
    > private:
    > static map<int,std::string> Queue_;
    > public:
    > std::string receive(int type_of_receive,int pid_r);
    > ....
    > };
    >
    > //cl.cpp
    > map<int,std::string> Cl::Queue_;
    > std::string receive(int type_of_receive,int pid_r) {
    > //Queue_ is used here
    > }
    >
    > //rrm.h
    > class Rrm
    > {
    > private:
    > Cl cl_ob_;
    > ....
    > };
    >
    > //rrm.cpp uses cl_ob_
    >
    > //sl.h
    > class Sl
    > {
    > private:
    > Cl cl_ob_;
    > ....
    > };
    > //sl.cpp uses cl_ob_
    >
    > main function is in some other file which uses rrm and sl header files.
    >
    > Problem: sl and rrm have different values for Queue_ .
    >
    > Help me out please.
    >


    I'd guess you're trying to make Queue_ a singleton. Look up singleton
    implementation techniques on google.

    The problem occurs because each compilation unit receives its own copy
    of Queue_. Try this instead:

    // cl.h
    extern map<int,std::string> Queue_;

    // cl.cpp
    map<int,std::string> Queue_

    If you fear this to qualify as an ugly global, then isolate it
    with a namespace.

    --
    OU
     
    Obnoxious User, Apr 4, 2008
    #3
  4. Martin York Guest

    On Apr 4, 7:54 am, Obnoxious User <O...@127.0.0.1> wrote:

    >
    > I'd guess you're trying to make Queue_ a singleton. Look up singleton
    > implementation techniques on google.
    >
    > The problem occurs because each compilation unit receives its own copy
    > of Queue_. Try this instead:


    NO.
    There is only one copy of Queue_ its in the cl.o (compiled from
    cl.cpp).


    >
    > // cl.h
    > extern map<int,std::string> Queue_;
    >
    > // cl.cpp
    > map<int,std::string> Queue_


    That does not help in any shape or form.

    You are making an object that was private to the class visible to
    everybody. This violates the whole premises of C++ where you keep
    variables only visible to people that need to know.
     
    Martin York, Apr 4, 2008
    #4
  5. On Fri, 04 Apr 2008 14:48:59 -0700, Martin York wrote:
    >> // cl.h
    >> extern map<int,std::string> Queue_;
    >>
    >> // cl.cpp
    >> map<int,std::string> Queue_

    >
    > That does not help in any shape or form.
    >
    > You are making an object that was private to the class visible to
    > everybody. This violates the whole premises of C++ where you keep
    > variables only visible to people that need to know.


    There are no premises to violate besides your own enforced design ideas.
    You can dislike it all you want.

    --
    OU
     
    Obnoxious User, Apr 4, 2008
    #5
  6. Martin York Guest

    On Apr 4, 3:01 pm, Obnoxious User <O...@127.0.0.1> wrote:

    > There are no premises to violate besides your own enforced design ideas.
    > You can dislike it all you want.
    >
    > --
    > OU


    The fact that it is a private data member indicates that Cl has some
    constraints on the Queue_ object that it maintains by only allowing
    access via a published interface. By making the object public you are
    allowing anybody unrestricted access to the object thus allowing
    (potential) incorrect usage that could eventually cause the
    application to crash. Thus you have potentially broken the code in a
    way that is nearly impossible to debug as the pre/post constraints are
    now no longer enforced by the compiler. Thus it is bad. i.e Think
    before you make a comment.
     
    Martin York, Apr 5, 2008
    #6
  7. Lionel B Guest

    On Fri, 04 Apr 2008 14:54:24 +0000, Obnoxious User wrote:

    > On Fri, 04 Apr 2008 04:21:10 -0700, sowmiya.ts wrote:
    >
    >> Hi,
    >>
    >> I have a header file cl.h which has a static variable queue.
    >>
    >> //cl.h
    >> class Cl
    >> {
    >> private:
    >> static map<int,std::string> Queue_;
    >> public:
    >> std::string receive(int type_of_receive,int pid_r);
    >> ....
    >> };
    >>
    >> //cl.cpp
    >> map<int,std::string> Cl::Queue_;


    [...]

    > The problem occurs because each compilation unit receives its own copy
    > of Queue_.


    ???

    Surely not?

    --
    Lionel B
     
    Lionel B, Apr 6, 2008
    #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. dwok
    Replies:
    7
    Views:
    705
    Hal Rosser
    Mar 4, 2005
  2. 0to60
    Replies:
    4
    Views:
    520
    jeffc
    Nov 21, 2003
  3. Siemel Naran
    Replies:
    4
    Views:
    810
    Micah Cowan
    Jan 12, 2005
  4. dolphin
    Replies:
    3
    Views:
    1,354
    Pete Becker
    Dec 5, 2007
  5. Replies:
    9
    Views:
    972
Loading...

Share This Page