I want to access a variable that is declared as such

class xx
{
private:
         CRITICAL_SECTION m_csResponse;
}

this variable I want to access from a method in another diff class in a different file(the variable being updated).What should I do in the other class?

Recommended Answers

All 3 Replies

You could make a friend class

You could make a friend class

Hey great?But how is this done?Could u give me the exact syntax.
For accessing var x in class xx from class yy

class xx
{
private:
CRITICAL_SECTION x;
}

class yy
{
SomeMethod(x++);
}

These classes are in different files(physically),then can U tell me how its done?
Thanks.

Here is an example using friend but I'll leave the rest up to you but you should be able to figure it out

#include <iostream>

class test;//forward declaration

class yep
{
  public:
    friend class test;//making "test" class a friend to this class so it can
    //modify its data
    int getX()
    { 
      return x;
    }
  private:
    int x;
};

class test
{
  public:
    //using the reference so it will actully change the info in the class
    void set(yep &testYep, int num)
    {
       testYep.x = num;//notice that I am accessing yep's private info
       //from test class
    }
};

    

int main(void)
{
  
  test mainTest;
  yep mainYep;
  
  mainTest.set(mainYep,10);
  
  std::cout<<mainYep.getX();
  std::cin.get();
  
  return 0;  
  
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.