2

I am accessing non static method from static Method. Below is the code. I read its bad design source. Why this bad design please help me to understand. If so how can one achieve it.

#include<iostream>

class Base 
{
public :
    static Base* initialiser;
    void BaseMethod() 
    {
        std::cout<<"Non static method Invoked"<<std::endl;
    }

    static  Base* GetInstance() 
    {
        if (!initialiser)
            initialiser = new Base();

        return initialiser;
    }
};

Base* Base::initialiser = nullptr;

class Service 
{
public: 
    void ServiceMethod()
    {
        Base::GetInstance()->BaseMethod();
    }
};

int main()
{
     Service obj;
     obj.ServiceMethod();
}
Federico
  • 743
  • 9
  • 22
rim
  • 351
  • 2
  • 15
  • 1
    I don't see anything wrong with your code. In any case most most design questions are either matters of opinion or dependent on circumstances. – john Jul 25 '19 at 06:17
  • 4
    Singletons and global state in general is arguably bad design. Calling a non-static member function from a static one is a minute cosmetic detail, not a design issue at all. – n. m. could be an AI Jul 25 '19 at 06:20
  • 1
    Your `GetInstance` method should use a function level static variable to store and initialise the singleton, this is simpler, less code and safer – Alan Birtles Jul 25 '19 at 06:32
  • 2
    Is there even a reason to use a singleton in this case? – Moia Jul 25 '19 at 06:48

2 Answers2

2

Why is accessing a non static method from static method is Bad design

It is not per se, as you are actually using a static member from a static methods

Yet this code snipped is too damn rigid, too damn over-engineered. Which means more likely to be buggy, to be a mess once a new requirement come into play.

  • defect : This code isn't thread safe. You should instantiate initializer not in GetInstance,rather using Base* Base::initialiser{new Base()};, which is guaranteed to be thread-safe from c++11.
  • defect : A class such like this should be derived with extreme caution. Or add final to prevent this possibility.
  • design : This code has still zero line of functionality. You are still plumbing. You want to reconsider if this is the best design for the problem being solved.
  • design : The purpose is to provide a singleton. We dev like to enforce unnecessary constraints such as uniqueness. When the time comes to support two instances in a system designed for a singleton, we have to refactor a whole lot of things.
UmNyobe
  • 22,539
  • 9
  • 61
  • 90
0

Since the question was for a general case, here is a quote from Wikipedia:

Static methods are meant to be relevant to all the instances of a class rather than to any specific instance.

A static method can be invoked even if no instances of the class exist yet.

Thus, you should consider static methods to be in class namespace meant for operations on a class rather than on instances/objects of the class.

In your case of making singleton, your are not accessing non-static method from a static one, but you are initializing an instance of that (static) object initialiser within the static method.

Community
  • 1
  • 1
karastojko
  • 1,156
  • 9
  • 14