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. a pointer question

a pointer question

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

    Point foobar() { Point local; Point *heap = new Point; *heap = local; // ... stuff ... delete heap; return local; } There is a question on the source above. Since point heap points to the object 'local', there is no need to new the point 'local' If I change 'Point * heap = new Point' to 'Point * heap', Isn't the source more effective? Another question is that if the class Point hasn't define the destructor function, We know that the compiler will define the default destructor function itself. But if I delete heap, will the call of default destructor be called? Many thanks. Tomorrow is another day.

    L N D 3 Replies Last reply
    0
    • R richardye

      Point foobar() { Point local; Point *heap = new Point; *heap = local; // ... stuff ... delete heap; return local; } There is a question on the source above. Since point heap points to the object 'local', there is no need to new the point 'local' If I change 'Point * heap = new Point' to 'Point * heap', Isn't the source more effective? Another question is that if the class Point hasn't define the destructor function, We know that the compiler will define the default destructor function itself. But if I delete heap, will the call of default destructor be called? Many thanks. Tomorrow is another day.

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

      richardye wrote:

      Since point heap points to the object 'local', there is no need to new the point 'local'

      heap does not point to local.

      richardye wrote:

      *heap = local;

      This is a copy by value

      richardye wrote:

      If I change 'Point * heap = new Point' to 'Point * heap', Isn't the source more effective?

      No, the copy by value above will fail

      richardye wrote:

      But if I delete heap, will the call of default destructor be called?

      yep

      R 1 Reply Last reply
      0
      • R richardye

        Point foobar() { Point local; Point *heap = new Point; *heap = local; // ... stuff ... delete heap; return local; } There is a question on the source above. Since point heap points to the object 'local', there is no need to new the point 'local' If I change 'Point * heap = new Point' to 'Point * heap', Isn't the source more effective? Another question is that if the class Point hasn't define the destructor function, We know that the compiler will define the default destructor function itself. But if I delete heap, will the call of default destructor be called? Many thanks. Tomorrow is another day.

        N Offline
        N Offline
        Naveen
        wrote on last edited by
        #3

        richardye wrote:

        If I change 'Point * heap = new Point' to 'Point * heap', Isn't the source more effective?

        Yes but in this case there should be a small change in the code. You should assign the address of local to heap. Point foobar() { Point local; Point *heap = new Point; *heap = local; Point *heap = &local; // ... stuff ... delete heap; return local; }

        richardye wrote:

        But if I delete heap, will the call of default destructor be called?

        Yes. But if you are modifying the code as i meantioned above you should not call delete.

        nave [OpenedFileFinder]

        N 1 Reply Last reply
        0
        • N Naveen

          richardye wrote:

          If I change 'Point * heap = new Point' to 'Point * heap', Isn't the source more effective?

          Yes but in this case there should be a small change in the code. You should assign the address of local to heap. Point foobar() { Point local; Point *heap = new Point; *heap = local; Point *heap = &local; // ... stuff ... delete heap; return local; }

          richardye wrote:

          But if I delete heap, will the call of default destructor be called?

          Yes. But if you are modifying the code as i meantioned above you should not call delete.

          nave [OpenedFileFinder]

          N Offline
          N Offline
          Nishad S
          wrote on last edited by
          #4

          Naveen.R wrote:

          Point *heap = &local;

          Provided he is not changing the value in the *heap.

          - NS -

          1 Reply Last reply
          0
          • L Lost User

            richardye wrote:

            Since point heap points to the object 'local', there is no need to new the point 'local'

            heap does not point to local.

            richardye wrote:

            *heap = local;

            This is a copy by value

            richardye wrote:

            If I change 'Point * heap = new Point' to 'Point * heap', Isn't the source more effective?

            No, the copy by value above will fail

            richardye wrote:

            But if I delete heap, will the call of default destructor be called?

            yep

            R Offline
            R Offline
            richardye
            wrote on last edited by
            #5

            thank you.

            1 Reply Last reply
            0
            • R richardye

              Point foobar() { Point local; Point *heap = new Point; *heap = local; // ... stuff ... delete heap; return local; } There is a question on the source above. Since point heap points to the object 'local', there is no need to new the point 'local' If I change 'Point * heap = new Point' to 'Point * heap', Isn't the source more effective? Another question is that if the class Point hasn't define the destructor function, We know that the compiler will define the default destructor function itself. But if I delete heap, will the call of default destructor be called? Many thanks. Tomorrow is another day.

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              richardye wrote:

              Since point heap points to the object 'local'...

              It would if you had done:

              Point *heap = &local;

              richardye wrote:

              ...there is no need to new the point 'local'

              And if you tried to, the compiler would complain because local is not a pointer.


              "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

              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