Solved

easy points class member access

Posted on 2000-03-06
4
140 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now