0

I'm getting my hands dirty with C++.

I come with a background in Java and C# and there's something that I don't think I understand in C++.

In these two lines:

Person kate("Kate");
Person& rKate = kate;

Does the fact that I'm using Person for one variable and Person& for the other, change in anyway how I can treat each variable later on? I understand the consequences in the calling code when declaring references as parameters of a function and then reassigning them in the function; and I understand the difference if I used a primitive type in the example above, but not for classes.

AxiomaticNexus
  • 6,190
  • 3
  • 41
  • 61
  • 3
    Read up on References, most good C++ books and references will cover the topic. – Thomas Matthews Jan 08 '15 at 19:26
  • References are much like pointers for most use cases. – OMGtechy Jan 08 '15 at 19:27
  • 1
    http://www.cprogramming.com/tutorial/references.html – Ricky Mutschlechner Jan 08 '15 at 19:27
  • 1
    In your code, `kate` and `rKate` are different names for the same object (`&kate == &rKate`). If you did `Person rKate = kate;` instead then you would be making a *copy* of the value in `kate` into a completely new `Person` object (`&kate != &rKate`). – cdhowie Jan 08 '15 at 19:29
  • @cdhowie: Why doesn't the close text contain a link to the duplicate question? I was in the midst of close voting as dup of http://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in?rq=1 – wallyk Jan 08 '15 at 19:30
  • @wallyk The link is up at the top of the question. – cdhowie Jan 08 '15 at 19:30
  • @cdhowie: Nevermind, I see it up top; I saw the lower notice first. – wallyk Jan 08 '15 at 19:31
  • 4
    "I understand the difference if I used a primitive type in the example above, but not for classes". It is exactly the same for primitive types and for classes. C++ does not give class types special treatment in the manner of Java and C#. – molbdnilo Jan 08 '15 at 19:31
  • @molbdnilo That answers my question. I had no idea that the `kate` object would be physically copied in memory just by reassigning it to another non-reference variable. – AxiomaticNexus Jan 08 '15 at 19:38

0 Answers0