0

I want to create a class, which will provide static method to create unique handler (migth be int, might float, might be something but always retrived as a pointer to object), but im still bit confused in my considerations, and when i started to read about singleton and factory pattern, then im confused at all.

Suppose i have a class

CHandle{
private:
        CHandle(const CHandle &hnd);
        CHandle &operator=(const CHandle &hnd);
        static int id;
public:
        static CHandle *createHandle(){
            id++;
            return this;
        }
}

in main i would use:

CHandle *c = CHandle::createHandle();

Can i do like that ? Or maybe Im messing up everything ?

JosiP
  • 2,619
  • 5
  • 29
  • 33
  • 1
    static methods have no knowledge of `this` – Borgleader Aug 05 '13 at 10:55
  • You could also have used the Search option: http://stackoverflow.com/questions/1008019/c-singleton-design-pattern – j4nSolo Aug 05 '13 at 11:03
  • 1
    Nooooooooooooooo! Do not use a singleton, they are evil! There are better ways to do this. Constructors and templates for starters. Why do you want to do this, or rather, what problem are you trying to solve? – Skizz Aug 05 '13 at 12:05
  • i want to have a mechanism, which can wrap different data type (like int, struct or whatever) and will return pointer to new unique object - like each time method createHanlde() will be called, a new unique pointer to object with unique id will be returned. Thats why i tought about singleton and a factory method, but then someone told me, that i can just use static method with a counter. But now im thinking who will destroy that objects ? – JosiP Aug 05 '13 at 12:21

3 Answers3

3

There is two thing :

  • First static methods have no knowledge of this. So no this inside static methods.
  • After you can do something like :

    CHandle{
    
    public:
            static CHandle *createHandle(){ 
                s_id++;
                return new CHandle(s_id);
            }
    private:
            CHandle(int id) :
                m_id(id)
            {};
    
            CHandle(const CHandle &hnd);
            CHandle &operator=(const CHandle &hnd);
    
            int m_id;         // Id of the instance
            static int s_id;
    
    };
    

And if you want it as a singleton :

CHandle{

public:
    static CHandle *createHandle(){
        if ( !s_Instance )
        {
            s_id++;
            s_Instance = new CHandle(s_id);
        }
        return s_Instance;
    }
private:
    CHandle(int id) :
        m_id(id)
    {};

    CHandle(const CHandle &hnd);
    CHandle &operator=(const CHandle &hnd);

    int m_id;         // Id of the instance
    static int s_id;
    static CHandle s_Instance;

};

don't forget in the .cpp :

int CHandle::s_id = 0;
CHandle* CHandle::s_Instance = NULL; // if singleton
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
0
CHandle{
private:
        CHandle(int id): m_id(id) {};
        CHandle(const CHandle &hnd);
        CHandle &operator=(const CHandle &hnd);
        int m_id;
        static int s_id;
public:
        static CHandle *createHandle(){
            s_id++;
            return new CHandle(s_id);
        }
}
umläute
  • 28,885
  • 9
  • 68
  • 122
0

Always remember that this is object related, but static is meant for whole class, so static methods can't access this.
Here is the help you need

CHandle{
private:
        CHandle(const CHandle &hnd);
        CHandle(int iidd);
        CHandle &operator=(const CHandle &hnd);
        static int id;
        static CHandle *obj;//Singleton object
public:
        static CHandle *createHandle(){
            id++;
            return obj;
        }

}

And in Project.cpp

int CHandle::id=0;
CHandle *CHandle::obj=new CHandle(xxx);
Govind Balaji
  • 639
  • 6
  • 17