Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8918269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:42:32+00:00 2026-06-15T05:42:32+00:00

We have a static class function in our code that houses a fair amount

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:42:33+00:00Added an answer on June 15, 2026 at 5:42 am

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this user defined function. public partial class UserDefinedFunctions { static int i;
In PHP, is it possible to have a function within a class that's non-static,
I have a static class that looks like this: namespace Argus { static class
So I have a static class that is supposed to be used as a
I have a static class that I use as my Data Utils for my
Is there a way to have a static class have static data that does
I have this code /////////////////////////////////////Gnome.cpp file #include Living.h class Gnome:public Living{ private: public: Gnome();
I have a code that we use many times with our apps, its a
I intended to create a class which only have static members and static functions.
I have a static class in a shared project, which I want to extend

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.