0

I try to find method how to get non-static variable from static func?

In static function, i want to get value from non-static variable.

It is possible?

class test {
private:
     static void test();
     string test;
}
3
  • 1
    Is this C++? Add C++-tag. Commented Oct 19, 2013 at 10:22
  • its not possible I recommend you to read bit more about statics Commented Oct 19, 2013 at 10:22
  • It's possible if you pass an actual test instance to your static function. Commented Oct 19, 2013 at 16:06

2 Answers 2

1

This is not possible. Static member functions can only access static class members. This makes sense, too, if you consider that you can invoke

test::test();

without instantiating an object. In fact, when executing the code above there may not even be a single class instance.

If you need to access non-static class members from a static member function you need to pass a class instance to it, either a pointer or a reference. When you're doing that, however, you might as well make the static member function non-static.

Sign up to request clarification or add additional context in comments.

Comments

0

You can't do that. When you use a static function, you are in static context. Only static members may be accessed, the others don't exist.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.