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
Post a Comment