Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ de-referencing between pointer

Say I have a class Foo

class Foo {
}

I do following assignments:

Foo *ptrFoo=new Foo();

Foo &ref=*(ptrFoo);  //question 1
Foo afoo=*(ptrFoo); //quesion 2

My questions :

1) When assignming to "&ref" what internally happens in-terms of memory? Is it just assigning the memory address of "ptrFoo" to "ref" ?

2) When assigning to "afoo", what happends? Does it call copy-constructor?That means memory is allocated for two Foo objects? ie, "afoo" and previously assigned memory for "ptrFoo" ?

3) Say I have a method called "void methodBar(const Foo &instance)" If I pass "ptrFoo" as:

methodBar( (*preFoo));

whats the significant of "const" here ?

like image 433
Ashika Umanga Umagiliya Avatar asked Nov 24 '11 05:11

Ashika Umanga Umagiliya


2 Answers

1) When assignming to "&ref" what internally happens in-terms of memory? Is it just assigning the memory address of "ptrFoo" to "ref" ?

That depends on your platform, compiler, and compiler settings. Your compiler may just generate a synonym for the dereferencing. Because a reference may not be redefined there's no reason a compiler really needs to allocate any memory for the variable.

2) When assigning to "afoo", what happends? Does it call copy-constructor?That means memory is allocated for two Foo objects? ie, "afoo" and previously assigned memory for "ptrFoo" ?

Yes, the contents of the Foo stored in dynamic storage are copied (using the copy constructor) to the instance of Foo in automatic storage. There's no dynamic allocation going on here though; the aFoo instance would get created just as simply if there were no assignment. For instance, Foo aFoo;.

3) Say I have a method called "void methodBar(const Foo &instance)" If I pass "ptrFoo" as: methodBar( (*preFoo)); whats the significant of "const" here ?

const in that position means that while the item is passed by reference, the method which declared that reference const is not allowed to modify the instance the reference references.

like image 72
Billy ONeal Avatar answered Sep 23 '22 10:09

Billy ONeal


  1. When doing Foo& ref = *ptrFoo;, you are making a reference to *ptrFoo. Only one Foo exists at this time. Any change you make to ref or *ptrFoo will affect the same object.

  2. When you do Foo afoo = *ptrFoo; (which is the same as Foo afoo = ref;), you create another seperate Foo, which exists independently of *ptrFoo. It is initialised by Foo's copy constructor. Note that afoo exists on the stack but *ptrFoo on the free store (heap). Any changes you make to *ptrFoo will not affect afoo, and vice versa. Also note that afoo will be destroyed and its memory freed automatically when it goes out of scope, but *ptrFoo must be destroyed and its memory freed explicitly by doing delete ptrFoo;

  3. The const in this case means that the function accepts a reference to a Foo which it promises not to modify. It cannot call any methods on that Foo that are not marked as const. Also, when you call this function, no new Foo is created (that is, it is not passed by value).

like image 44
Seth Carnegie Avatar answered Sep 20 '22 10:09

Seth Carnegie