Lannie

Hello,
How do I access the values of a variable from another class (ex. access variable int m_x from class CX in class CY)

thanks!


Re: Visual C++ General Variable from another class

PareshC

Make that variable as public (access specifier). However this is not the usual practive. You should provide GetX() and SetX() methods to access the variable.

Code Block

int CX::GetX()

{

return m_x;

}

void CX::SetX(int x)

{

m_x = x;

}

Regards

Paresh.





Re: Visual C++ General Variable from another class

Lannie

I've already tried that one, but I get the error "illegal call of non-static member function" whenever I try to call CX::GetX or CX:Tongue TiedetX from another class.




Re: Visual C++ General Variable from another class

PareshC

How are you calling the function

Is your situation like http://msdn2.microsoft.com/en-us/library/ms858871.aspx

Please refer below URLs for static data member and static member functions,

http://msdn2.microsoft.com/hi-in/library/b1b5y48f(VS.80).aspx

http://msdn2.microsoft.com/hi-in/library/yyh8hetw(VS.80).aspx

Regards,

Paresh.





Re: Visual C++ General Variable from another class

Andreas Masur

Lannie wrote:
Hello,
How do I access the values of a variable from another class (ex. access variable int m_x from class CX in class CY)

Well...how are these two classes related Are they derived from each other Is one contained in the other






Re: Visual C++ General Variable from another class

Ajay Sinha

Lannie wrote:
I've already tried that one, but I get the error "illegal call of non-static member function" whenever I try to call CX::GetX or CX:Tongue TiedetX from another class.


I think GetX and SetX are non-static functions inside class CX. In that case you can't call these functions like the way you are calling. First you need to make an instance of CX and then call these functions through that object.

CX obj;
obj.GetX();