Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Private Destructor

Private Destructor

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    San 0
    wrote on last edited by
    #1

    Hi all, What is the use of declaring a private destructor? Where we are using this? Can anybody tell an example Thanks San

    _ M 2 Replies Last reply
    0
    • S San 0

      Hi all, What is the use of declaring a private destructor? Where we are using this? Can anybody tell an example Thanks San

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      The purpose of the private destructor is to avoid the deletion of the object while it is still referenced

      Some things seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

      S 1 Reply Last reply
      0
      • _ _AnsHUMAN_

        The purpose of the private destructor is to avoid the deletion of the object while it is still referenced

        Some things seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

        S Offline
        S Offline
        San 0
        wrote on last edited by
        #3

        Could please send me some sample code

        1 Reply Last reply
        0
        • S San 0

          Hi all, What is the use of declaring a private destructor? Where we are using this? Can anybody tell an example Thanks San

          M Offline
          M Offline
          Manish K Agarwal
          wrote on last edited by
          #4

          and other use could be if you want to stop the creation of the object on stack. Yes, you can not delete these object by using delete. Either you have to assume that you don't want to delete such objects or you have to overload the delete or delete these from any friend function. class Test { public: Test(){} private: ~Test(){} }; int _tmain(int argc, _TCHAR* argv[]) { //Test tmp; // Compilation error; Test *testP = new Test(); // work fine return 0; }

          Manish Agarwal manish.k.agarwal @ gmail DOT com

          S 1 Reply Last reply
          0
          • M Manish K Agarwal

            and other use could be if you want to stop the creation of the object on stack. Yes, you can not delete these object by using delete. Either you have to assume that you don't want to delete such objects or you have to overload the delete or delete these from any friend function. class Test { public: Test(){} private: ~Test(){} }; int _tmain(int argc, _TCHAR* argv[]) { //Test tmp; // Compilation error; Test *testP = new Test(); // work fine return 0; }

            Manish Agarwal manish.k.agarwal @ gmail DOT com

            S Offline
            S Offline
            sheshidar
            wrote on last edited by
            #5

            Test *testP = new Test(); // work fine

            what about delete?

            M 1 Reply Last reply
            0
            • S sheshidar

              Test *testP = new Test(); // work fine

              what about delete?

              M Offline
              M Offline
              Manish K Agarwal
              wrote on last edited by
              #6

              for delete we have to create a friend function or we have to assume that we don't want to delete this :)

              Manish Agarwal manish.k.agarwal @ gmail DOT com

              K 1 Reply Last reply
              0
              • M Manish K Agarwal

                for delete we have to create a friend function or we have to assume that we don't want to delete this :)

                Manish Agarwal manish.k.agarwal @ gmail DOT com

                K Offline
                K Offline
                krishna_CP
                wrote on last edited by
                #7

                HI Manish, How to write Friend Function to call Delete on Private Destructor. Thanks, Krishna.

                M 1 Reply Last reply
                0
                • K krishna_CP

                  HI Manish, How to write Friend Function to call Delete on Private Destructor. Thanks, Krishna.

                  M Offline
                  M Offline
                  Manish K Agarwal
                  wrote on last edited by
                  #8

                  here is a minimal example

                  class A
                  {
                  private:
                  ~A(){};
                  public:
                  A(){};
                  friend void fn();
                  };

                  void fn()
                  {
                  A a;
                  }

                  In this case if you create a object of class A on stack in any other function other than fn() complier will give you an error C2248: 'A::~A' : cannot access private member declared in class 'A'

                  Manish Agarwal manish.k.agarwal @ gmail DOT com

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups