We have a static class function in our code that houses a fair amount of code. Where this code was originally used, and still is used, no instance of the class can be created hence why it is static. The functionality of this function is now needed elsewhere in our codebase, where an instance of the class is already created.
Without making a non-static and static version of the same function is there anyway we can create a non-static function that houses all the code that can be polled using the static function in places where no class instance can be initialized, while allowing it to be called using the actual instance elsewhere.
For example
#include <iostream>
class Test
{
public:
Test(){};
~Test(){};
void nonStaticFunc(bool calledFromStatic);
static void staticFuncCallingNonStaticFunc();
};
void Test::nonStaticFunc(bool calledFromStatic)
{
std::cout << "Im a non-static func that will house all the code" << std::endl;
if(calledFromStatic)
// do blah
else
// do blah
}
void Test::staticFuncCallingNonStaticFunc()
{
std::cout << "Im a static func that calls the non static func that will house all `the code" << std::endl;
nonStaticFunc(true);
}
int main(int argc, char* argv[])
{
// In some case this could be called as this
Test::staticFuncCallingNonStaticFunc();
// in others it could be called as
Test test;
test.nonStaticFunc(false);
}
Depending on if its call statically or not the code may alter slightly within the non static function, so we cant simply use a static function at all times, because sometimes we will need access to non-static members used elsewhere in the code. Most of the code will remain identical however. Cheers