c++ - Call non-static method from base class in static create method -



c++ - Call non-static method from base class in static create method -

i want write static create method, phone call non-static method base of operations class.

<baseclass.h> class baseclass { public: void method(); } <myclass.h> class myclass : public baseclass { static myclass* createmyclass(); } <myclass.cpp> ... myclass* myclass::createmyclass() { myclass* myclass = new myclass(); method(); // error, illegal phone call of non-static fellow member function homecoming myclass; } ...

so have phone call base of operations class method outside of createmyclass method, or there possible way phone call inside?

non-static methods need invoked on instance, , compiler doesn't pretend smart plenty know instance want invoke on (unless in instance method). need explicitly invoke method on instance of myclass created:

myclass->method();

(another way of thinking it: in non-static context, calling method using syntax method(); equivalent this->method();. since don't have "this" due beingness in static context, need supply "this" yourself.)

c++

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -