accessing static member variables

S

sowmiya.ts

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
 
L

Lionel B

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.
 
O

Obnoxious User

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.
 
M

Martin York

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.
 
O

Obnoxious User

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.
 
M

Martin York

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

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.
 
L

Lionel B

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top