Solved

easy points class member access

Posted on 2000-03-06
4
145 Views
Last Modified: 2010-04-02
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
Comment
Question by:pellep
  • 2
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2587682
>> Can you access private memeber data/functions
>> from static member function
Yes.

Anything more?


It is done often.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2587697
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
 
LVL 4

Author Comment

by:pellep
ID: 2587698
Thanks. Short and to the point (I guess easy questions merits easy answers).
0
 
LVL 4

Author Comment

by:pellep
ID: 2587711
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question