Single instance issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sarath

    #1

    Single instance issue

    I've to write a single instance class. there are different methods to
    control the single instance of a program. I've tried the following
    method

    class CSingleton
    {
    public:
    CSingleton& GetInstance(){ static CSingleton s; return s; }

    private:
    CSingleton(){}
    ~CSingleton(){}
    }


    The above code failed to compile in Visual C++ 6.0 but compiled in
    Visual C++ 7.1 and Visual C++ Express 2008. CRT (in microsoft concept)
    calling the destructor of the class. So that Visual C++ 6.0
    compilation error (Failed to access destructor) is correct according
    to the concept.

    I also tried in Dev C++. It was successful but didn't call the dtor of
    the class. Which implementation is correct according to the standard.

    Sorry if off-topic to the forum
  • Ian Collins

    #2
    Re: Single instance issue

    Sarath wrote:
    I've to write a single instance class. there are different methods to
    control the single instance of a program. I've tried the following
    method
    >
    class CSingleton
    {
    public:
    CSingleton& GetInstance(){ static CSingleton s; return s; }
    >
    How can you call this method and how would you change its definition if
    you did want to call it?
    private:
    CSingleton(){}
    ~CSingleton(){}
    }
    >
    It wouldn't compile with any compiler without the missing semicolon.
    The above code failed to compile in Visual C++ 6.0
    Never trust that compiler, it's old and not very standards compliant.

    --
    Ian Collins.

    Comment

    • Sarath

      #3
      Re: Single instance issue

      On Dec 21, 4:49 pm, Ian Collins <ian-n...@hotmail.co mwrote:
      Sarath wrote:
      I've to write a single instance class. there are different methods to
      control the single instance of a program. I've tried the following
      method
      >
      class CSingleton
      {
      public:
          CSingleton& GetInstance(){ static CSingleton s; return s; }
      >
      How can you call this method and how would you change its definition if
      you did want to call it?
      >
      private:
          CSingleton(){}
          ~CSingleton(){}
      }
      >
      It wouldn't compile with any compiler without the missing semicolon.
      >
      The above code failed to compile in Visual C++ 6.0
      >
      Never trust that compiler, it's old and not very standards compliant.
      >
      --
      Ian Collins.
      Dear All,
      I'm extremely sorry to paste wrong code. Please refer this one.

      class CSingle
      {
      public:
      static CSingle& GetInstance(){ static CSingle s; return s; }

      private:
      CSingle() { cout<<"ctor"; }
      ~CSingle() { cout<<"dtor"; }
      };

      Sorry for the incovenience.Pl ease refer this code for my question

      Regards,
      Sarath

      Comment

      • Salt_Peter

        #4
        Re: Single instance issue

        On Dec 21, 3:11 am, Sarath <CSar...@gmail. comwrote:
        On Dec 21, 4:49 pm, Ian Collins <ian-n...@hotmail.co mwrote:
        >
        >
        >
        Sarath wrote:
        I've to write a single instance class. there are different methods to
        control the single instance of a program. I've tried the following
        method
        >
        class CSingleton
        {
        public:
        CSingleton& GetInstance(){ static CSingleton s; return s; }
        >
        How can you call this method and how would you change its definition if
        you did want to call it?
        >
        private:
        CSingleton(){}
        ~CSingleton(){}
        }
        >
        It wouldn't compile with any compiler without the missing semicolon.
        >
        The above code failed to compile in Visual C++ 6.0
        >
        Never trust that compiler, it's old and not very standards compliant.
        >
        --
        Ian Collins.
        >
        Dear All,
        I'm extremely sorry to paste wrong code. Please refer this one.
        >
        class CSingle
        {
        public:
        static CSingle& GetInstance(){ static CSingle s; return s; }
        >
        private:
        CSingle() { cout<<"ctor"; }
        ~CSingle() { cout<<"dtor"; }
        >
        };
        >
        Sorry for the incovenience.Pl ease refer this code for my question
        >
        Regards,
        Sarath
        declare your destructor public. No reason to hide it.
        So what about the compiler generated copy constructor?

        int main()
        {
        CSingle instance;
        CSingle copy(instance);
        }

        and assignment? etc...



        Comment

        • Sarath

          #5
          Re: Single instance issue

          On Dec 21, 5:41 pm, Salt_Peter <pj_h...@yahoo. comwrote:
          On Dec 21, 3:11 am, Sarath <CSar...@gmail. comwrote:
          >
          >
          >
          On Dec 21, 4:49 pm, Ian Collins <ian-n...@hotmail.co mwrote:
          >
          Sarath wrote:
          I've to write a single instance class. there are different methods to
          control the single instance of a program. I've tried the following
          method
          >
          class CSingleton
          {
          public:
          CSingleton& GetInstance(){ static CSingleton s; return s; }
          >
          How can you call this method and how would you change its definition if
          you did want to call it?
          >
          private:
          CSingleton(){}
          ~CSingleton(){}
          }
          >
          It wouldn't compile with any compiler without the missing semicolon.
          >
          The above code failed to compile in Visual C++ 6.0
          >
          Never trust that compiler, it's old and not very standards compliant.
          >
          --
          Ian Collins.
          >
          Dear All,
          I'm extremely sorry to paste wrong code. Please refer this one.
          >
          class CSingle
          {
          public:
          static CSingle& GetInstance(){ static CSingle s; return s; }
          >
          private:
          CSingle() { cout<<"ctor"; }
          ~CSingle() { cout<<"dtor"; }
          >
          };
          >
          Sorry for the incovenience.Pl ease refer this code for my question
          >
          Regards,
          Sarath
          >
          declare your destructor public. No reason to hide it.
          So what about the compiler generated copy constructor?
          >
          int main()
          {
          CSingle instance;
          CSingle copy(instance);
          >
          }
          >
          and assignment? etc...
          It will work if I make the dtor public. But I just want to know about
          the destruction of static objects in the above scenario. As All
          compilers behaves in different manner.

          Regards,
          Sarath

          Comment

          • James Kanze

            #6
            Re: Single instance issue

            On Dec 21, 9:41 am, Salt_Peter <pj_h...@yahoo. comwrote:
            On Dec 21, 3:11 am, Sarath <CSar...@gmail. comwrote:
            I'm extremely sorry to paste wrong code. Please refer this one.
            class CSingle
            {
            public:
            static CSingle& GetInstance(){ static CSingle s; return s; }
            private:
            CSingle() { cout<<"ctor"; }
            ~CSingle() { cout<<"dtor"; }
            };
            Sorry for the incovenience.Pl ease refer this code for my question
            declare your destructor public. No reason to hide it.
            There's also no reason to make it public, since the code should
            work if the destructor is private as well. (A long time ago,
            this was a frequent error, since the pre-standard specification
            wasn't too clear as to where access of the destructor should be
            checked. But any modern compiler should get it right.)
            So what about the compiler generated copy constructor?
            Good point.
            int main()
            {
            CSingle instance;
            CSingle copy(instance);
            }
            and assignment? etc...
            Assignment would require two instances, or... Making it private
            certainly doesn't hurt, however, and IMHO makes the intent
            clearer.

            --
            James Kanze (GABI Software) email:james.kan ze@gmail.com
            Conseils en informatique orientée objet/
            Beratung in objektorientier ter Datenverarbeitu ng
            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

            Comment

            Working...