0

Could anyone please explain all the different types of ways to pass by parameters? Specifically, involving ampersands ('&') and the 'const' keyword. I've seen many kinds, but I don't really understand what they do, and when they should be used.

To list a few: Type & const Type const & const Type &

Also, I'm not sure what syntax to use. For example, for a parameter of type int & const called a, is it written as int & a const or int & const a? For an int const & parameter called a, is it written int const & a or int const a &?

Here is what I understand. I understand how to create a reference. I know that a reference is like an alias to an address, and does not make a copy of the parameter. I also get that the ampersand symbol can sometimes mean addresses.

If anyone could clarify this, I would really appreciate it.

  • 6
    Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Mar 24 '23 at 14:42
  • 4
    There's really only *one* way to pass any argument by reference. And that's to add the ampersand `&` in the declaration. That's it really. The `const` and its placement just adds that the object being referenced might not be modified. The `const` qualifier adds some other semantics (like being able to bind the reference to an rvalue) but that doesn't really change the "pass object by reference" semantics. – Some programmer dude Mar 24 '23 at 14:45
  • 2
    As for placement, `const T&` and `T const&` are both the same. The first (`const T&`) means "a reference to a `T` object, where the object is constant"; The second (`T const&`) means "a reference to a a constant object of type `T`". Both are equivalent. Something like `T& const` or `T& x const` are simply not valid. – Some programmer dude Mar 24 '23 at 14:47
  • ***I also get that the ampersand symbol can sometimes mean addresses.*** It's not a reference in that case its the address of operator. Maybe this clarifies some of your concern about that: [https://stackoverflow.com/questions/35594378/address-of-operator-vs-reference-operator](https://stackoverflow.com/questions/35594378/address-of-operator-vs-reference-operator) – drescherjm Mar 24 '23 at 15:06
  • @Someprogrammerdude thanks! Do you know a good source to learn more about l/rvalues, universal references and the like? – ericssonl07 Mar 24 '23 at 15:06
  • This is a good reference: [https://www.amazon.com/C-Programming-Language-4th/dp/0321563840](https://www.amazon.com/C-Programming-Language-4th/dp/0321563840) – drescherjm Mar 24 '23 at 15:08
  • I really recommend you invest in one or more of the books from the list linked by @NathanOliver. Sure, some might be expensive, but they will teach you much better than many online-resources. And if you spread out the cost over the years you will have use of that knowledge, it really won't be that much. In short, they are worth their weight in gold. – Some programmer dude Mar 24 '23 at 16:43

0 Answers0