• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 154
  • Last Modified:

easy points class member access

Can you access private memeber data/functions from static member function

class X
{
private:
  int MyVal
public:
static void MyStatic(X *cls)
{
  cls->MyVal=10;
}
void DoStaticStuff()
{
  X::MyStatic(this);
}
};
int main()
{
  X MyX
  MyX.DoStaticStuff();
}
0
pellep
Asked:
pellep
  • 2
  • 2
1 Solution
 
nietodCommented:
>> Can you access private memeber data/functions
>> from static member function
Yes.

Anything more?


It is done often.
0
 
nietodCommented:
Actually, I wasn't reading quite carefully enough, the way in which you are doing it is a little unusall, but legal.  When I said it was done often I was thinking you were accessing a private _static_ data member.  That is more common.

Out of curiousity, why did you code it that way?  i.e why make MyStatic() static.  That would be a good funciton to have as non-staitc.  Was it just for an example?
0
 
pellepAuthor Commented:
Thanks. Short and to the point (I guess easy questions merits easy answers).
0
 
pellepAuthor Commented:
The reason for the odd construction is deliberate. MyStatic is used in a call to CreateThread(), passing 'this' as the startup argument. (of course, in real life it would look sligthly different).
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now