I am a novice C ++ programmer and do PoC for my company. Therefore, I apologize for my main 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 would be greatly appreciated.
thank
=== UPDATE ===
This can be seen as a scenario in which I am developing an application in QT Creator, where I have a static function of a predefined signature and you want to access user interface elements for changing text (for example, TextEdit)
You cannot do this, even in Java.
staticmethods are just local helper functions for all instances of a class without access to a separate state of the class (for example, t2).
static
t2
static , - a static, , .
EDIT:
, SDK , , t2. public static. , staticFunctionToAccessT2 :
public
staticFunctionToAccessT2
public: void mutateT2();
, mutateT2 on, :
mutateT2
TestOne foo;
SDK , :
std::bind(&TestOne::mutateT2, foo)
, , SDK std::function, .
std::function
- " ".
, Singleton (anti-), Singleton ++. , , , -.
, RAII , std::unique_ptr<TestOne> safeInstance;, getInstance(), Android.
std::unique_ptr<TestOne> safeInstance;
getInstance()
Singleton, , . , , .
, "", , "", singleton, , . singleton.
; -.
, API ( ), . ; , :
static TestOne * bodge; static void staticFunctionToAccessT2(){ TestTwo * t2 = bodge->t2; }
API
TestOne::bodge = my_t1; broken_api(&TestOne::staticFunctionToAccessT2);
bodge - , API , .
bodge
API , " "; , void*, . :
void*
static void staticFunctionToAccessT2(void * p) { TestOne * t1 = static_cast<TestOne*>(p); TestTwo * t2 = t1->t2; } less_broken_api(&TestOne::staticFunctionToAccessT2, my_t1);
API , :
void memberFunctionToAccessT2() { TestTwo * t2 = this->t2; } nice_api([=]{my_t1->memberFunctionToAccessT2();});