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. Do I need to delete?

Do I need to delete?

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 3 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.
  • G Offline
    G Offline
    gumi_r msn com
    wrote on last edited by
    #1

    Hi all, my question is if I define a class myClass with destructor, assign operator (=) and copy constructor, etc etc. If I later use my class like this: myClass &a=*new myClass(whatever); myClass &b=a; Do I have to explicitly delete b? I've read you should only delete a class if you have instantiated it with a new. I know that behind the curtains b is created with a new call inside the dll, but do I have to delete it explicitly from my app? thanks

    CPalliniC 1 Reply Last reply
    0
    • G gumi_r msn com

      Hi all, my question is if I define a class myClass with destructor, assign operator (=) and copy constructor, etc etc. If I later use my class like this: myClass &a=*new myClass(whatever); myClass &b=a; Do I have to explicitly delete b? I've read you should only delete a class if you have instantiated it with a new. I know that behind the curtains b is created with a new call inside the dll, but do I have to delete it explicitly from my app? thanks

      CPalliniC Online
      CPalliniC Online
      CPallini
      wrote on last edited by
      #2

      gumi_r@msn.com wrote:

      myClass &a=*new myClass(whatever); myClass &b=a;

      since you've dynamically allocated memory for an istance of myClass, then you have to delete it somewhere. You can call either delete &b or delete &a (of course don't call both). Cheers :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

      In testa che avete, signor di Ceprano?

      G 1 Reply Last reply
      0
      • CPalliniC CPallini

        gumi_r@msn.com wrote:

        myClass &a=*new myClass(whatever); myClass &b=a;

        since you've dynamically allocated memory for an istance of myClass, then you have to delete it somewhere. You can call either delete &b or delete &a (of course don't call both). Cheers :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        G Offline
        G Offline
        gumi_r msn com
        wrote on last edited by
        #3

        Ok thanks. That makes sense as I'm already deleting any pointers I need to inside my copy constructor. Next step would be the assignment operator. Say for example y have: myClass &a=*new myClass(whatever); myClass &b=*new myClass(whatever); myClass &c=a+b; do I have to delete &c? thanks for any replies.

        CPalliniC 1 Reply Last reply
        0
        • G gumi_r msn com

          Ok thanks. That makes sense as I'm already deleting any pointers I need to inside my copy constructor. Next step would be the assignment operator. Say for example y have: myClass &a=*new myClass(whatever); myClass &b=*new myClass(whatever); myClass &c=a+b; do I have to delete &c? thanks for any replies.

          CPalliniC Online
          CPalliniC Online
          CPallini
          wrote on last edited by
          #4

          gumi_r@msn.com wrote:

          myClass &c=a+b;

          IMHO you should not do this, because you're trying to assign a reference to a temporary object. For instance, the following is noy allowed(the compiler complains about):

          int &i= *new int(5);
          int &j=i;
          int &k=i+j;// ERROR

          BTW: Doing some experimentation? :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

          In testa che avete, signor di Ceprano?

          S 1 Reply Last reply
          0
          • CPalliniC CPallini

            gumi_r@msn.com wrote:

            myClass &c=a+b;

            IMHO you should not do this, because you're trying to assign a reference to a temporary object. For instance, the following is noy allowed(the compiler complains about):

            int &i= *new int(5);
            int &j=i;
            int &k=i+j;// ERROR

            BTW: Doing some experimentation? :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #5

            It is not illegal if you use a const reference.

            Steve

            CPalliniC 1 Reply Last reply
            0
            • S Stephen Hewitt

              It is not illegal if you use a const reference.

              Steve

              CPalliniC Online
              CPalliniC Online
              CPallini
              wrote on last edited by
              #6

              Yeah, I didn't know it. Thank you (though seems rather clumsy...).

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

              In testa che avete, signor di Ceprano?

              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