Help with singleton



  • In my singleton class, I am getting errors:

    -/home/matheus/Dropbox/Projetos/Freedom/TecTracker/Core/dataaccess/dataaccess.cpp:28: error: ISO C++ forbids declaration of 'instance' with no type [-fpermissive]
    DataAccess::instance()
    ^-

    /home/matheus/Dropbox/Projetos/Freedom/TecTracker/Core/dataaccess/dataaccess.cpp:28: error: prototype for 'int DataAccess::instance()' does not match any in class 'DataAccess'
    DataAccess::instance()
    ^

    /home/matheus/Dropbox/Projetos/Freedom/TecTracker/Core/dataaccess/dataaccess.cpp:21: In file included from ../../../TecTracker/Core/dataaccess/dataaccess.cpp:21:0:

    /home/matheus/Dropbox/Projetos/Freedom/TecTracker/Core/dataaccess/dataaccess.hpp:29: error: candidate is: static DataAccess* DataAccess::instance()
    static DataAccess* instance(); /* <= Devolve um objeto do tipo DataAccess*/
    ^

    Below the code:

    @/Classe singleton/

    #ifndef DATAACCESS_HPP
    #define DATAACCESS_HPP

    class DataAccess
    {
    public:
    static DataAccess* instance(); /* <= Devolve um objeto do tipo DataAccess*/

    private:
    DataAccess();
    DataAccess(const DataAccess&);
    DataAccess& operator=(const DataAccess&);
    static DataAccess* dataaccess; /* <= Objeto DataAccess*/
    };

    #endif // DATAACCESS_HPP@

    @#include "dataaccess.hpp"

    DataAccess::DataAccess()
    {
    }

    DataAccess::instance()
    {

    if(!dataaccess)
    dataaccess = new DataAccess;

    return dataaccess;
    }
    @



  • I think you need to change:
    @DataAccess::instance()
    {

    if(!dataaccess)
    dataaccess = new DataAccess;

    return dataaccess;
    }@

    To:
    @DataAccess *DataAccess::instance()
    {

    if(!dataaccess)
    dataaccess = new DataAccess;

    return dataaccess;
    }@



  • I like to use the lazy singleton method when using it for logging, then you don't have to worry about the pointer allocation and memory cleanup

    class definition in .h
    @
    class Singleton
    {
    public:
    static Singleton& instance();

    private:
    Singleton();
    Singleton& operator = (const Singleton&);
    ~Singleton();

     static Singleton mInstance;
    

    }
    @

    in .cpp
    @
    Singleton Singleton::mInstance;

    Singleton& Singleton::instance()
    {
    return mInstance;
    }

    Singleton::~Singleton()
    {
    }
    @

    Its all a matter of preference though.



  • Thank boys

    dvez43

    I do not understand your code. What line you instantiate the object? And where do you test if the object already exists? I do not recognize this kind of singleton.



  • @ /Classe singleton/

    #ifndef DATAACCESS_HPP
    #define DATAACCESS_HPP
     
    class DataAccess
    {
    public:
      static DataAccess *instance(); 
    

    static uninit();
    ~DataAccess();

    private:
      DataAccess();
      DataAccess(const DataAccess&);
      DataAccess& operator=(const DataAccess&);
      static DataAccess *dataaccess;
    };
     
    #endif // DATAACCESS_HPP
    

    @

    @ #include "dataaccess.hpp"
    DataAccess DataAccess::dataaccess = NULL;

    DataAccess::DataAccess()
    {
    }
     
    DataAccess::instance()
    {
     
      if(dataaccess == NULL){
        dataaccess = new DataAccess;
    

    }

      return dataaccess;
    }
    

    DataAccess::uninit(){
    if (dataaccess !=NULL){
    delete dataaccess;
    dataaccess = NULL
    }

    DataAccess::~DataAccess(){
    delete dataaccess;
    }
    @



  • You do not need to instantiate the object if it is a static object as a member variable. Singleton Singleton::mInstance; in the cpp makes sure the object is created... everything is allocated on the stack.

    The only to access the object publicly is through the instance function which returns the public member by reference.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.