How to create singleton class inline

This is a discussion on How to create singleton class inline within the C++ Programming forums, part of the General Programming Boards category; i have a working example for a singleton class file exmpl.hpp Code: class cX { private: static cX *iX; public: ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    7

    How to create singleton class inline

    i have a working example for a singleton class

    file exmpl.hpp
    Code:
    class cX
    {
      private:
      static cX *iX;
      public:
      static cX* Create()
      {
        if(iX==NULL)
        {
          iX=new cX();
        }
        return iX;
      };
    };
    and
    file: exmpl.cpp
    Code:
    cX* cX::iX=NULL;  // INIT <- THIS
    somebody an idea how to put that together into 1 header?
    Last edited by NA9x2000; 08-24-2012 at 10:00 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    You cannot put the contents of exmpl.cpp in the header because it contains a variable definition (that of the static member pointer). If the header were included in more than one source file, the variable would be defined more than once, which is an error.

    By the way, it is more conventional to name Create as Get since the object is only created once, and transparently to the caller.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    7
    i'm using #ifndef __c_X__ #define __c_X__ ... class code etc ... #endif in each header that should avoid that redeclaration or not? just a q cause i didnt mentioned that before but i think u know what ure talking about ;-)

    ure right - when i try to set the iX outside the class at the top compiler is missing constructor and destructor and below compiler says multiple definition

    i asked for a solution i probably didnt think about

    thanks again ;-)
    Last edited by NA9x2000; 08-24-2012 at 10:43 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by NA9x2000
    i'm using #ifndef __c_X__ #define __c_X__ #endif in each header that should avoid that redeclaration or not?
    No, it won't. It would only help avoid the problem when the header is included multiple times in the same source file.

    By the way, names that begin with an underscore followed by an uppercase letter, or that contain consecutive underscores, are reserved to the (compiler and standard library) implementation for any use. As such, your proposed __c_C__ identifier is reserved and hence you should not be defining it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,280
    O_o

    If you want to convince me that you need a singleton (versus simply declaring a global instance of a class), I'll happily tell you how to do it "inline".

    Soma

  6. #6
    Registered User
    Join Date
    Aug 2012
    Posts
    7
    oh buddy what are u waiting for?? xD tell me plz

    i'll remove that underscores thx LL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class vs. Namespace as Singleton
    By eros in forum C++ Programming
    Replies: 0
    Last Post: 03-28-2012, 06:07 AM
  2. Singleton Class error
    By anirban in forum C++ Programming
    Replies: 10
    Last Post: 10-28-2010, 06:44 AM
  3. Compiler complaining about my singleton class
    By DavidP in forum C++ Programming
    Replies: 12
    Last Post: 06-09-2009, 02:30 AM
  4. Bug in Singleton class
    By marquito in forum C++ Programming
    Replies: 17
    Last Post: 12-14-2007, 12:02 PM
  5. singleton class problem
    By ... in forum C++ Programming
    Replies: 6
    Last Post: 12-22-2003, 05:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21