-1

I'm a rusty amateur C++ programmer coming back after along time and moving on from OpenGL 2.0 to Vulkan.

I'm trying to understand the code behind the tutorials I'm reading not just copy, but don't understand the & part of the following:

catch (const std::exception& e) {
    std::cerr << e.what() << std::endl;
    return EXIT_FAILURE;
}

Why is the & after exception? My understanding is the pointer is usually after the variable and the address before it, such as:

int* pintVar;
pintVar = &intVar;

Has this got something to do with binding a constant reference 'exception' to a temporary object 'e'?

As you can see I'm trying to research/google it but not totally understanding it or why.

Any help would be appreciated.

MrBenjamin
  • 57
  • 4
  • 1
    It declares a variable of reference type. Any basic C++ book should be explaining this, it's an important part of the type system – UnholySheep May 14 '20 at 14:31
  • In this case, it does not indicate a pointer, but a reference, this means you can pass a value into this function and it will update the value even when it's not returned by the function, in this way it is like passing a pointer to a function, but the type is defined by the declaration. – Yvain May 14 '20 at 14:31
  • Are you asking for *a)* an explanation as to why `&` is to the right of `std::exception` as opposed to being to the left or *b)* are you asking what `&` means or *c)* are you asking about both *a)* and *b)*? – Joakim Thorén May 14 '20 at 14:39

2 Answers2

4

& has different meanings depending on context. In a declaration, it means reference to. e.g.

int a = 42;
int &b = a;  // b is a reference to a 
             // b is essentially an alias of a (they both have the same address, for example)
             // i.e. changes to b will be reflected in a, and vice versa

Here's some reference that explains this in more depth.

When & is used on an already existing variable, this means address-of. e.g.

int a = 42;
int *b = &a; // b points to a (i.e. b holds the address of a).

So in your example, e is a const reference to whatever exception is passed in (since the parameter of a function declares a variable).

cigien
  • 57,834
  • 11
  • 73
  • 112
  • this is a good answer but for the fact that it doesn't explain what a reference is. – Yvain May 14 '20 at 14:35
  • 1
    Added a bit more explanation. – cigien May 14 '20 at 14:36
  • Using `l-value` in you explanation is maybe not appropriate here, given that @Ben is a *rusty amateur*. I think a simple explanation of `l-value` is in place (perhaps with a reference to a more technical explanation should anyone be interested), or omit mentioning `l-value` altogether and explain it in layman-terms instead. – Joakim Thorén May 14 '20 at 14:45
  • Amateur - self taught C++, rusty - been programming vba for past 6 years. Understand the reference, similarities to vba's ByRef in functions (in my head anyways:D). Get the & after the exception due to the const declaration. Now looking up 1-value. Cheers for the info guys, really useful. – MrBenjamin May 14 '20 at 14:54
  • @Ben Great, glad it helped :) – cigien May 14 '20 at 14:55
  • I suggest reading this thread, apart from the obvious battle between old school and new school programmers that prefer to use respectively pointers and references,everything is well explained. https://stackoverflow.com/questions/334856/are-there-benefits-of-passing-by-pointer-over-passing-by-reference-in-c?rq=1 – Yvain May 14 '20 at 16:33
  • @MrBenjamin Since the answer helped, consider [accepting](https://stackoverflow.com/help/someone-answers) it. – cigien May 14 '20 at 18:47
-1

Analogous question:

int* pintVar;

"Why is the * after int? What is the indirection operator doing there? Why isn't it before the variable name such as in here?"

int intVar = *pintVar;

Well, you see * symbol has multiple meanings. Perhaps you already understand this: Within a compound type name, it signifies a pointer type. Within an expression it signifies an operator. It can be either the unary indirection operator (*pintVar) or the binary multiplication operator (a * b).

In the exactly same way, & symbol has multiple meanings. Within a compound type name, it signifies a reference type. Within an expression it signifies an operator. It can be either the unary addressof operator (&intVar) or the binary bitwise AND operator (a & b).


So, const T& is a reference to const T, just like const T* would be a pointer to const T.

Both reference and pointer are forms of indirection, and are quite smilar. Their differences are:

  • Pointers are objects, references are not objects
    • Because references are not objects, there is no way to take the address of a reference, and there is no such thing as a pointer to reference or a reference to reference.
    • There are also no arrays of references.
  • A (non-const) pointer can be assigned to point to another object. A reference is bound to a single object for its entire lifetime.
  • Pointer can be null, a reference cannot be.
  • You must indirect through a pointer explicitly using an indirection operator. All operations on a reference implicitly indirect through the reference, and apply to the referred object instead.
    • Assignment of reference is assignment of the referred object.
    • Taking the address of a reference variable is taking address of the referred object.
  • Pointers are iterators for arrays; You can do pointer arithmetic to iterate the elements. Adding one to a pointer results in pointer to the successive array element. There is no reference arithmetic. Because of the implicit indirection, adding one to reference adds one to the referred object.

Has this got something to do with binding a constant reference 'exception' to a temporary object 'e'?

There are many right words there, but I don't understand what you are trying to mean by them.

e is a variable. The type of the variable is const std::exception& i.e. reference to constant exception. Upon throw, the reference is bound to the exception object that was thrown.

eerorika
  • 232,697
  • 12
  • 197
  • 326