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. new operator not understood

new operator not understood

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 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.
  • P Offline
    P Offline
    prithaa
    wrote on last edited by
    #1

    Hello, If I have a class class A { int *myint; }; I can initialize the above member variable like this int *myint = new int; so that a pointer is initialized on the heap and I will have to delete it. But if I have to initialize the member variable in the constructor of a class how should I do it using new? Thanks Prithaa

    C T 2 Replies Last reply
    0
    • P prithaa

      Hello, If I have a class class A { int *myint; }; I can initialize the above member variable like this int *myint = new int; so that a pointer is initialized on the heap and I will have to delete it. But if I have to initialize the member variable in the constructor of a class how should I do it using new? Thanks Prithaa

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Exactly the same but in the constructor:

      A::A()
      {
      myint = new int;
      }

      Don't forget to destroy it in the destructor of your class then.


      Cédric Moonen Software developer
      Charting control [v1.1]

      L 1 Reply Last reply
      0
      • P prithaa

        Hello, If I have a class class A { int *myint; }; I can initialize the above member variable like this int *myint = new int; so that a pointer is initialized on the heap and I will have to delete it. But if I have to initialize the member variable in the constructor of a class how should I do it using new? Thanks Prithaa

        T Offline
        T Offline
        ThatsAlok
        wrote on last edited by
        #3

        prithaa wrote:

        int *myint;

        you can  try auto_ptr too .. http://visualcpp.org/template_permalink.asp?id=126[^]

        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

        cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

        S 1 Reply Last reply
        0
        • T ThatsAlok

          prithaa wrote:

          int *myint;

          you can  try auto_ptr too .. http://visualcpp.org/template_permalink.asp?id=126[^]

          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

          cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

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

          You could use a std::auto_ptr but should beware of the transfer of ownership on copy semantics. This could mean, for example, that an assignment breaks the object on the right hand side.

          Steve

          P 1 Reply Last reply
          0
          • S Stephen Hewitt

            You could use a std::auto_ptr but should beware of the transfer of ownership on copy semantics. This could mean, for example, that an assignment breaks the object on the right hand side.

            Steve

            P Offline
            P Offline
            prithaa
            wrote on last edited by
            #5

            Hello, Thanks I got it. Prithaa

            1 Reply Last reply
            0
            • C Cedric Moonen

              Exactly the same but in the constructor:

              A::A()
              {
              myint = new int;
              }

              Don't forget to destroy it in the destructor of your class then.


              Cédric Moonen Software developer
              Charting control [v1.1]

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Although you can simply write,

              myint = new int;

              Be aware while using assignment operator or copy constructor. If you are not cautious, will end up with dangling pointers.

              C 1 Reply Last reply
              0
              • L Lost User

                Although you can simply write,

                myint = new int;

                Be aware while using assignment operator or copy constructor. If you are not cautious, will end up with dangling pointers.

                C Offline
                C Offline
                Cedric Moonen
                wrote on last edited by
                #7

                :confused: WTF ?? This was posted more than 3 years ago !! Furthermore, you are replying to me and not to the OP.

                Cédric Moonen Software developer
                Charting control [v3.0] OpenGL game tutorial in C++

                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