Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling non-static variable from static function

Tags:

c++

I am a beginner programmer in C++ and doing a PoC for my company. So I apologize for my basic question.

class TestOne{
  private:
    TestTwo* t2;

  void createInstance(TestTwo* param){
    t2 = param;
  }

  static void staticFunctionToAccessT2(){
    // Now here I want to access "t2" here in the current instance of the class
    // By current instance I mean "this" in non-static context
    // currently there is no function to get object, but can be created
    // ** we cannot call new TestOne(), because that will create a new instance
    // ** of the current class and that I don't want.
  }
}

Any help in this regard will be greatly appreciated.

Thanks

===UPDATE===

This can be taken as a scenario in which I am developing an application in QT Creator, where I have a static function of predefined signature and want to access the UI elements for text change(like TextEdit)

like image 743
Jayesh Avatar asked Nov 14 '14 12:11

Jayesh


People also ask

Can we call non static variable in static method?

Yes, a static method can access a non-static variable. This is done by creating an object to the class and accessing the variable through the object. In the below example main is a static method which accesses variable a which is a non-static variable.

How do you reference a non static variable from a static context?

In a simple way, we have to create an object of the class to refer to a non-static variable from a static context. A new copy of all the non-static variables is created when a new instance of variable is created. So, we can access these variables by using the reference of the new instance of the class.

Can a static function call a non static function in C?

A static method provides NO reference to an instance of its class (it is a class method) hence, no, you cannot call a non-static method inside a static one.

Can we use a non static data member within a static member function?

You cannot have static and nonstatic member functions with the same names and the same number and type of arguments. Like static data members, you may access a static member function f() of a class A without using an object of class A .


1 Answers

You can't do this, even in Java.

static methods are simply local helper functions for all instances of the class with no access to individual class state (for example t2).

Either remove the static from the method, or make the member variable a static variable, depending on what you are trying to accomplish.

EDIT:

If I understand you correctly your SDK wants a function pointer which it will call to modify your instances' t2. Your mutator method should be public and non-static. So let's just say you redefined staticFunctionToAccessT2 like this:

public: void mutateT2();

If the instance you wanted to call mutateT2 on was defined as:

TestOne foo;

Where your SDK wants a function pointer you could pass this in:

std::bind(&TestOne::mutateT2, foo)

As is pointed out by Mike Seymour below this only works if the SDK method parameter is a std::function not if its parameter is a raw function pointer.

like image 119
Jonathan Mee Avatar answered Oct 04 '22 22:10

Jonathan Mee