Implementing the singleton pattern [closed]












-1















using namespace std;

class PersonA{
private:
PersonA(){cout << "PersonA cconstr called" << endl;};

public:
PersonA* createInstance(){
cout << "Instance created" << endl;
return new PersonA;
};
};

int main()
{
PersonA* Hello = PersonA::createInstance();
return 0;
}


My IDE is giving me an error which says:
Call to non static member function without an argument
I dont understand why this method requires an argument?
Am i missing something?










share|improve this question













closed as off-topic by Sneftel, t.niese, Raedwald, DebanjanB, zx485 Jan 1 at 20:55


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Sneftel, t.niese, zx485

If this question can be reworded to fit the rules in the help center, please edit the question.












  • 1





    createInstance is not a static function so you cant call it without an object.

    – user1438832
    Jan 1 at 5:22











  • ohhhhhhh, thank you

    – LuckyBlue
    Jan 1 at 5:24











  • I am abit drunk, but thank you

    – LuckyBlue
    Jan 1 at 5:24











  • How come is this a singleton pattern?

    – Kunal Puri
    Jan 1 at 5:27











  • It is beginning to look like a singleton pattern

    – LuckyBlue
    Jan 1 at 5:30
















-1















using namespace std;

class PersonA{
private:
PersonA(){cout << "PersonA cconstr called" << endl;};

public:
PersonA* createInstance(){
cout << "Instance created" << endl;
return new PersonA;
};
};

int main()
{
PersonA* Hello = PersonA::createInstance();
return 0;
}


My IDE is giving me an error which says:
Call to non static member function without an argument
I dont understand why this method requires an argument?
Am i missing something?










share|improve this question













closed as off-topic by Sneftel, t.niese, Raedwald, DebanjanB, zx485 Jan 1 at 20:55


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Sneftel, t.niese, zx485

If this question can be reworded to fit the rules in the help center, please edit the question.












  • 1





    createInstance is not a static function so you cant call it without an object.

    – user1438832
    Jan 1 at 5:22











  • ohhhhhhh, thank you

    – LuckyBlue
    Jan 1 at 5:24











  • I am abit drunk, but thank you

    – LuckyBlue
    Jan 1 at 5:24











  • How come is this a singleton pattern?

    – Kunal Puri
    Jan 1 at 5:27











  • It is beginning to look like a singleton pattern

    – LuckyBlue
    Jan 1 at 5:30














-1












-1








-1








using namespace std;

class PersonA{
private:
PersonA(){cout << "PersonA cconstr called" << endl;};

public:
PersonA* createInstance(){
cout << "Instance created" << endl;
return new PersonA;
};
};

int main()
{
PersonA* Hello = PersonA::createInstance();
return 0;
}


My IDE is giving me an error which says:
Call to non static member function without an argument
I dont understand why this method requires an argument?
Am i missing something?










share|improve this question














using namespace std;

class PersonA{
private:
PersonA(){cout << "PersonA cconstr called" << endl;};

public:
PersonA* createInstance(){
cout << "Instance created" << endl;
return new PersonA;
};
};

int main()
{
PersonA* Hello = PersonA::createInstance();
return 0;
}


My IDE is giving me an error which says:
Call to non static member function without an argument
I dont understand why this method requires an argument?
Am i missing something?







c++






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 5:20









LuckyBlueLuckyBlue

24




24




closed as off-topic by Sneftel, t.niese, Raedwald, DebanjanB, zx485 Jan 1 at 20:55


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Sneftel, t.niese, zx485

If this question can be reworded to fit the rules in the help center, please edit the question.







closed as off-topic by Sneftel, t.niese, Raedwald, DebanjanB, zx485 Jan 1 at 20:55


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Sneftel, t.niese, zx485

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1





    createInstance is not a static function so you cant call it without an object.

    – user1438832
    Jan 1 at 5:22











  • ohhhhhhh, thank you

    – LuckyBlue
    Jan 1 at 5:24











  • I am abit drunk, but thank you

    – LuckyBlue
    Jan 1 at 5:24











  • How come is this a singleton pattern?

    – Kunal Puri
    Jan 1 at 5:27











  • It is beginning to look like a singleton pattern

    – LuckyBlue
    Jan 1 at 5:30














  • 1





    createInstance is not a static function so you cant call it without an object.

    – user1438832
    Jan 1 at 5:22











  • ohhhhhhh, thank you

    – LuckyBlue
    Jan 1 at 5:24











  • I am abit drunk, but thank you

    – LuckyBlue
    Jan 1 at 5:24











  • How come is this a singleton pattern?

    – Kunal Puri
    Jan 1 at 5:27











  • It is beginning to look like a singleton pattern

    – LuckyBlue
    Jan 1 at 5:30








1




1





createInstance is not a static function so you cant call it without an object.

– user1438832
Jan 1 at 5:22





createInstance is not a static function so you cant call it without an object.

– user1438832
Jan 1 at 5:22













ohhhhhhh, thank you

– LuckyBlue
Jan 1 at 5:24





ohhhhhhh, thank you

– LuckyBlue
Jan 1 at 5:24













I am abit drunk, but thank you

– LuckyBlue
Jan 1 at 5:24





I am abit drunk, but thank you

– LuckyBlue
Jan 1 at 5:24













How come is this a singleton pattern?

– Kunal Puri
Jan 1 at 5:27





How come is this a singleton pattern?

– Kunal Puri
Jan 1 at 5:27













It is beginning to look like a singleton pattern

– LuckyBlue
Jan 1 at 5:30





It is beginning to look like a singleton pattern

– LuckyBlue
Jan 1 at 5:30












2 Answers
2






active

oldest

votes


















1














static is missing.



but you don't implement singleton pattern currently, if you want to, use Meyers' one:



class PersonA
{
private:
PersonA() { std::cout << "PersonA constr called" << std::endl;}
public:
PersonA(const PersonA&) = delete;
PersonA& operator =(const PersonA&) = delete;

static PersonA& getInstance(){
static PersonA instance{};
return instance;
}
};


But maybe you just want a factory method, then avoid raw owning pointer, and use smart pointer:



class PersonA{
private:
PersonA() { std::cout << "PersonA constr called" << std::endl;}

public:
std::unique_ptr<PersonA> create()
{
std::cout << "Instance created" << std::endl;
return std::make_unique<PersonA>();
}
};





share|improve this answer

































    1














    you can do it like -



    #include <iostream>

    using namespace std;

    class PersonA{
    private:
    PersonA(){cout << "PersonA cconstr called" << endl;};
    static PersonA* p;
    public:
    static PersonA* createInstance(){
    if(p == nullptr)
    {
    cout << "Instance created" << endl;
    p= new PersonA();
    }
    return p;
    };
    };

    PersonA* PersonA::p=nullptr;

    int main()
    {
    PersonA* A = PersonA::createInstance();
    PersonA* B = PersonA::createInstance();
    return 0;
    }





    share|improve this answer
























    • Thank you thank you, god bless. Happy new year

      – LuckyBlue
      Jan 1 at 5:31











    • Attempting to code when drunk is not good

      – LuckyBlue
      Jan 1 at 5:31











    • But happy new year anyway!!

      – LuckyBlue
      Jan 1 at 5:31











    • BIG UP MAN!!!!!!!

      – LuckyBlue
      Jan 1 at 5:42











    • You will also want to delete the copy constructor and assignment operator.

      – user4581301
      Jan 1 at 5:44




















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    static is missing.



    but you don't implement singleton pattern currently, if you want to, use Meyers' one:



    class PersonA
    {
    private:
    PersonA() { std::cout << "PersonA constr called" << std::endl;}
    public:
    PersonA(const PersonA&) = delete;
    PersonA& operator =(const PersonA&) = delete;

    static PersonA& getInstance(){
    static PersonA instance{};
    return instance;
    }
    };


    But maybe you just want a factory method, then avoid raw owning pointer, and use smart pointer:



    class PersonA{
    private:
    PersonA() { std::cout << "PersonA constr called" << std::endl;}

    public:
    std::unique_ptr<PersonA> create()
    {
    std::cout << "Instance created" << std::endl;
    return std::make_unique<PersonA>();
    }
    };





    share|improve this answer






























      1














      static is missing.



      but you don't implement singleton pattern currently, if you want to, use Meyers' one:



      class PersonA
      {
      private:
      PersonA() { std::cout << "PersonA constr called" << std::endl;}
      public:
      PersonA(const PersonA&) = delete;
      PersonA& operator =(const PersonA&) = delete;

      static PersonA& getInstance(){
      static PersonA instance{};
      return instance;
      }
      };


      But maybe you just want a factory method, then avoid raw owning pointer, and use smart pointer:



      class PersonA{
      private:
      PersonA() { std::cout << "PersonA constr called" << std::endl;}

      public:
      std::unique_ptr<PersonA> create()
      {
      std::cout << "Instance created" << std::endl;
      return std::make_unique<PersonA>();
      }
      };





      share|improve this answer




























        1












        1








        1







        static is missing.



        but you don't implement singleton pattern currently, if you want to, use Meyers' one:



        class PersonA
        {
        private:
        PersonA() { std::cout << "PersonA constr called" << std::endl;}
        public:
        PersonA(const PersonA&) = delete;
        PersonA& operator =(const PersonA&) = delete;

        static PersonA& getInstance(){
        static PersonA instance{};
        return instance;
        }
        };


        But maybe you just want a factory method, then avoid raw owning pointer, and use smart pointer:



        class PersonA{
        private:
        PersonA() { std::cout << "PersonA constr called" << std::endl;}

        public:
        std::unique_ptr<PersonA> create()
        {
        std::cout << "Instance created" << std::endl;
        return std::make_unique<PersonA>();
        }
        };





        share|improve this answer















        static is missing.



        but you don't implement singleton pattern currently, if you want to, use Meyers' one:



        class PersonA
        {
        private:
        PersonA() { std::cout << "PersonA constr called" << std::endl;}
        public:
        PersonA(const PersonA&) = delete;
        PersonA& operator =(const PersonA&) = delete;

        static PersonA& getInstance(){
        static PersonA instance{};
        return instance;
        }
        };


        But maybe you just want a factory method, then avoid raw owning pointer, and use smart pointer:



        class PersonA{
        private:
        PersonA() { std::cout << "PersonA constr called" << std::endl;}

        public:
        std::unique_ptr<PersonA> create()
        {
        std::cout << "Instance created" << std::endl;
        return std::make_unique<PersonA>();
        }
        };






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 1 at 17:15

























        answered Jan 1 at 9:12









        Jarod42Jarod42

        118k12103188




        118k12103188

























            1














            you can do it like -



            #include <iostream>

            using namespace std;

            class PersonA{
            private:
            PersonA(){cout << "PersonA cconstr called" << endl;};
            static PersonA* p;
            public:
            static PersonA* createInstance(){
            if(p == nullptr)
            {
            cout << "Instance created" << endl;
            p= new PersonA();
            }
            return p;
            };
            };

            PersonA* PersonA::p=nullptr;

            int main()
            {
            PersonA* A = PersonA::createInstance();
            PersonA* B = PersonA::createInstance();
            return 0;
            }





            share|improve this answer
























            • Thank you thank you, god bless. Happy new year

              – LuckyBlue
              Jan 1 at 5:31











            • Attempting to code when drunk is not good

              – LuckyBlue
              Jan 1 at 5:31











            • But happy new year anyway!!

              – LuckyBlue
              Jan 1 at 5:31











            • BIG UP MAN!!!!!!!

              – LuckyBlue
              Jan 1 at 5:42











            • You will also want to delete the copy constructor and assignment operator.

              – user4581301
              Jan 1 at 5:44


















            1














            you can do it like -



            #include <iostream>

            using namespace std;

            class PersonA{
            private:
            PersonA(){cout << "PersonA cconstr called" << endl;};
            static PersonA* p;
            public:
            static PersonA* createInstance(){
            if(p == nullptr)
            {
            cout << "Instance created" << endl;
            p= new PersonA();
            }
            return p;
            };
            };

            PersonA* PersonA::p=nullptr;

            int main()
            {
            PersonA* A = PersonA::createInstance();
            PersonA* B = PersonA::createInstance();
            return 0;
            }





            share|improve this answer
























            • Thank you thank you, god bless. Happy new year

              – LuckyBlue
              Jan 1 at 5:31











            • Attempting to code when drunk is not good

              – LuckyBlue
              Jan 1 at 5:31











            • But happy new year anyway!!

              – LuckyBlue
              Jan 1 at 5:31











            • BIG UP MAN!!!!!!!

              – LuckyBlue
              Jan 1 at 5:42











            • You will also want to delete the copy constructor and assignment operator.

              – user4581301
              Jan 1 at 5:44
















            1












            1








            1







            you can do it like -



            #include <iostream>

            using namespace std;

            class PersonA{
            private:
            PersonA(){cout << "PersonA cconstr called" << endl;};
            static PersonA* p;
            public:
            static PersonA* createInstance(){
            if(p == nullptr)
            {
            cout << "Instance created" << endl;
            p= new PersonA();
            }
            return p;
            };
            };

            PersonA* PersonA::p=nullptr;

            int main()
            {
            PersonA* A = PersonA::createInstance();
            PersonA* B = PersonA::createInstance();
            return 0;
            }





            share|improve this answer













            you can do it like -



            #include <iostream>

            using namespace std;

            class PersonA{
            private:
            PersonA(){cout << "PersonA cconstr called" << endl;};
            static PersonA* p;
            public:
            static PersonA* createInstance(){
            if(p == nullptr)
            {
            cout << "Instance created" << endl;
            p= new PersonA();
            }
            return p;
            };
            };

            PersonA* PersonA::p=nullptr;

            int main()
            {
            PersonA* A = PersonA::createInstance();
            PersonA* B = PersonA::createInstance();
            return 0;
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 1 at 5:29









            user1438832user1438832

            39318




            39318













            • Thank you thank you, god bless. Happy new year

              – LuckyBlue
              Jan 1 at 5:31











            • Attempting to code when drunk is not good

              – LuckyBlue
              Jan 1 at 5:31











            • But happy new year anyway!!

              – LuckyBlue
              Jan 1 at 5:31











            • BIG UP MAN!!!!!!!

              – LuckyBlue
              Jan 1 at 5:42











            • You will also want to delete the copy constructor and assignment operator.

              – user4581301
              Jan 1 at 5:44





















            • Thank you thank you, god bless. Happy new year

              – LuckyBlue
              Jan 1 at 5:31











            • Attempting to code when drunk is not good

              – LuckyBlue
              Jan 1 at 5:31











            • But happy new year anyway!!

              – LuckyBlue
              Jan 1 at 5:31











            • BIG UP MAN!!!!!!!

              – LuckyBlue
              Jan 1 at 5:42











            • You will also want to delete the copy constructor and assignment operator.

              – user4581301
              Jan 1 at 5:44



















            Thank you thank you, god bless. Happy new year

            – LuckyBlue
            Jan 1 at 5:31





            Thank you thank you, god bless. Happy new year

            – LuckyBlue
            Jan 1 at 5:31













            Attempting to code when drunk is not good

            – LuckyBlue
            Jan 1 at 5:31





            Attempting to code when drunk is not good

            – LuckyBlue
            Jan 1 at 5:31













            But happy new year anyway!!

            – LuckyBlue
            Jan 1 at 5:31





            But happy new year anyway!!

            – LuckyBlue
            Jan 1 at 5:31













            BIG UP MAN!!!!!!!

            – LuckyBlue
            Jan 1 at 5:42





            BIG UP MAN!!!!!!!

            – LuckyBlue
            Jan 1 at 5:42













            You will also want to delete the copy constructor and assignment operator.

            – user4581301
            Jan 1 at 5:44







            You will also want to delete the copy constructor and assignment operator.

            – user4581301
            Jan 1 at 5:44





            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            Npm cannot find a required file even through it is in the searched directory