Reputation: 4096
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
Upvotes: 0
Views: 3339
Reputation: 208323
I am inclined not to provide a workaround, as I believe this is a design issue that should be fixed, rather than hacked to work.
At any rate, you can factor the common code into a static function that takes a pointer to an object. When calling it from the non-static member you pass this
while when calling from a static function you don't pass the object:
class test {
void impl( test* p ) {
// ...
if (p) { // we have an object: use it
p->dosomething();
}
}
public:
void nonStaticVersion() {
impl(this);
}
static void staticVersion() {
impl(0);
}
};
But you should reconsider whether you really want to do this. Think on what impl
does, give it a name. If you cannot find a simple name and short explanation of what it does, refactor until you have functions that perform simple tasks that are easily described. Note that if the description of a function starts having conditions, that is a code smell (i.e. do X or Y depending on Z and if... is a hint that the function does not have a clear responsibility.
Upvotes: 2
Reputation: 74018
Refactor common parts into a class method and call that from both methods. You cannot access non-static members in your common parts method, of course.
class Test
{
public:
Test(){};
~Test(){};
void nonStaticFunc();
static void staticFunc();
private:
static void commonParts();
};
void Test::commonParts()
{
std::cout << "Im a static func that will house all common parts" << std::endl;
// do common stuff
}
void Test::nonStaticFunc()
{
std::cout << "Im a non-static func that will call the common parts and do other things then" << std::endl;
commonParts();
// do non-static stuff
}
void Test::staticFunc()
{
std::cout << "Im a static func that will call the common parts and then do other things" << std::endl;
commonParts();
// do static stuff
}
int main(int argc, char* argv[])
{
// In some case this could be called as this
Test::staticFunc();
// in others it could be called as
Test test;
test.nonStaticFunc();
}
Upvotes: 5