Thread: How to create singleton class inline

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    5,108
    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, 01:02 PM
  5. singleton class problem
    By ... in forum C++ Programming
    Replies: 6
    Last Post: 12-22-2003, 06:16 PM