0

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

6
  • 2
    Static is a Class member not an object's member. You must review your design. Commented Nov 30, 2012 at 14:12
  • 1
    Don't do that! If you have different behaviour, then refactor common parts into a separate method and call this in your static or non-static function respectively. Commented Nov 30, 2012 at 14:14
  • I thought about doing that, but the common parts would need to be declared static to be accessed from both I would assume? Commented Nov 30, 2012 at 14:15
  • 1
    @Moore91 Yes, of course. But that isn't a problem, because you can call a static method from a non-static method, but not the other way. Commented Nov 30, 2012 at 14:17
  • @JoachimPileborg how would you call the static function with an instance as the argument? Commented Nov 30, 2012 at 14:21

2 Answers 2

5

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();
}
Sign up to request clarification or add additional context in comments.

5 Comments

Even though this is a good solution, it doesnt help in this situation because the subtle changes I need make use of non static members that cannot be accessed within a static function. I also cannot make these static members
@Moore91 You cannot use non-static members without an object. So, when you insist on using a non-static method in your class method, you must create at least a temporary object.
I only use non-static members when an instance of the class is created. When it is run statically without an instance these non-static members would not be used. I was hoping I could share the code in a non-static function that can then be polled using a static function.
@Moore91 Then I haven't understood, why you can't refactor the common parts into another method. If the code for static and non-static is that different, however subtle, you have to use two different methods. I can't tell you more without seeing the concrete source.
Yeh Ill just use two different methods first and can optimize it later. The function is pretty complex so I don't want to risk breaking it since it works perfectly as it is atm. Thanks for your help anyway
2

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.

1 Comment

Yeh I Agree that this is an issue that really needs to be fixed. thanks for your response anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.