PDA

View Full Version : To Share a variable among classes



JenniferF
12th May 2011, 07:12
//A.h
class A
{
public:
int quantity[8];
int m;

}

//B.h
class B
{
public:
void getquantity ();


}

//B.cpp

void B::getquantity()
{
//how can I get access to quantity or any variables from A? when I try A::m- I get errors
}

Lykurg
12th May 2011, 07:36
make m static,
get a private instance of A in B
use a singleton pattern
etc...

high_flyer
12th May 2011, 12:13
@JenniferF:
This is a symptom of bad design, since it breaks OOP principals.
It usually should not come to this with a correct design.
Lykurgs suggestions are correct, but the fact they need to be used is already the problem.
If you explain what it is you want to achieve, we probably can offer you better design approach.