|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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_ . me out please. Thanks, Sowmiya |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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_ . > > 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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|