?
Solved

easy points class member access

Posted on 2000-03-06
4
Medium Priority
?
149 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 200 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

752 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