PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > accessing static member variables
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
accessing static member variables

Réponse
 
LinkBack Outils de la discussion
Vieux 04/04/2008, 12h21   #1
sowmiya.ts@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut accessing static member variables

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
  Réponse avec citation
Vieux 04/04/2008, 13h16   #2
Lionel B
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: accessing static member variables

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
  Réponse avec citation
Vieux 04/04/2008, 15h54   #3
Obnoxious User
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: accessing static member variables

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
  Réponse avec citation
Vieux 04/04/2008, 22h48   #4
Martin York
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: accessing static member variables

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.



  Réponse avec citation
Vieux 04/04/2008, 23h01   #5
Obnoxious User
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: accessing static member variables

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
  Réponse avec citation
Vieux 05/04/2008, 01h32   #6
Martin York
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: accessing static member variables

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.
  Réponse avec citation
Vieux 06/04/2008, 13h03   #7
Lionel B
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: accessing static member variables

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h04.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16804 seconds with 15 queries