Solved

easy points class member access

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

738 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