954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to access a variable of a diff class

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?

arjunmukherjee
Newbie Poster
2 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

You could make a friend class

prog-bman
Junior Poster
109 posts since Nov 2004
Reputation Points: 14
Solved Threads: 4
 
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.

arjunmukherjee
Newbie Poster
2 posts since Dec 2004
Reputation Points: 10
Solved Threads: 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;  
  
}
prog-bman
Junior Poster
109 posts since Nov 2004
Reputation Points: 14
Solved Threads: 4
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You