mercredi 24 juillet 2019

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

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();
}

Aucun commentaire:

Enregistrer un commentaire