0

I'm following a tutorial and I have this line:

Human(const std::string & name) : nameC(name) { }

My question

  • Here what does & (ampersand) do?
  • What does nameC(name) do? nameC is a variable itself, isn't it?
yano
  • 4,095
  • 3
  • 35
  • 68
Onur Can
  • 57
  • 1
  • 1
  • 8
  • The `&` in this context means `name` is a reference to a `std::string` object. `nameC(name)` is in the constructor's *member intialization list*, so it is initializing the `nameC` class member with the value of `name`. This stuff is covered in any decent C++ book/tutorial, please do some research before asking questions. – Remy Lebeau Mar 31 '18 at 00:40
  • @RemyLebeau thanks for the comment. I've found lots of sources about oop in c++ however I'm too lazy to read hundreds of pages on a computer. I'm following this tutorial https://www.codeproject.com/articles/835135/object-oriented-programming-with-cplusplus – Onur Can Mar 31 '18 at 00:49
  • What tutorial are you following? Does it not explain these things? – Galik Mar 31 '18 at 00:50
  • 1
    Don't use online tutorials for C++. Those are generally garbage. Get a [good book](https://stackoverflow.com/q/388242/9254539) instead. – eesiraed Mar 31 '18 at 02:03
  • I know. That's what I'm plannign to do :) – Onur Can Mar 31 '18 at 05:57

0 Answers0