0

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?

Edited by happygeek: fixed formatting

2
Contributors
3
Replies
4
Views
12 Years
Discussion Span
Last Post by prog-bman
0

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.

0

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;  
  
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.