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