0

I got the following from Meyer's More Effective C++:

string s1("Nancy");
string s2("Clancy");

string& rs = s1;     // rs refers to s1
string *ps = &s1;    // ps points to s1

rs = s2;             // rs still refers to s1, but s1’s value is now "Clancy"
ps = &s2;            // ps now points to s2; s1 is unchanged

I do not understand why one line dereferences the pointer an assigns to the address of s1, to "point to" s1:

string *ps = &s1;

yet another line doesn't de-reference the pointer to "point to" s2:

ps = &s2;

can someone help me out here? Its the fact two different conventions seem to be doing the same thing which is confusing me.

intrigued_66
  • 16,082
  • 51
  • 118
  • 189
  • 1
    This is just because it's an initialization during the variable declaration. This would have been valid as well: `string *ps; ps = &s1;` – Nbr44 Jun 04 '13 at 08:33
  • 1
    Reference and pointer are two different things. Please see http://stackoverflow.com/questions/57483/what-are-the-differences-between-pointer-variable-and-reference-variable-in-c?rq=1 – Dariusz Jun 04 '13 at 08:33
  • Nbr44 is right. See http://www.cplusplus.com/doc/tutorial/pointers/ pls :) – bartimar Jun 04 '13 at 08:40
  • 1
    Read the items explaining difference between *initialization* and *assignment* – Balog Pal Jun 04 '13 at 10:35

3 Answers3

6

string *ps = &s1;

In my opinion, this is better written as string* ps = &s1;, but they mean the same thing to the compiler. You're creating a new variable "ps" of type "string*" (pointer to string), and assigning it an initial value equal to the address of the s1 variable. There's absolutely no dereferencing being done... the * in this usage indicates a pointer type, not a dereferencing operation.

ps = &s2; then assigns a new value to the ps pointer, namely the address of the s2 string object.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • 1
    Why is putting the * next to the type better? Now someone will write `string* ps = &s1, p2 = &s2;` and be confused – harold Jun 04 '13 at 08:44
  • 2
    @harold: yes - it's an age-old conflict, with no perfect solution. Personally, I optimise my code style for the *much* more common case, and generally break the mixed pointers + non-pointers in your example onto separate lines to avoid the potential confusion you mention. – Tony Delroy Jun 04 '13 at 08:49
1
string *ps = &s1;

sets ps to the address of s1

ps = &s2;

sets ps to the address of s2.

A reference can ONLY be set on the same line as the variable is defined. Anything after that copies the right value into the referenced value on the left.

Pointers can be set as much and often as you like, and the access the content of a pointer, you need to use *ps = ... or ps[x] (in this case, anything other than 0 for x will be undefined)

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0
rs = s2;

In this line rs is a reference not a pointer. When s2 is assigned with rs, rs still points to same memory but the value at that pointer changes.

ps = &s2;

Here ps is a pointer, here address of s2 is assigned to ps, earlier ps was pointing to different location that location itself is being moved to point to some other memory.

Hence the change in behavior for pointer and reference.