Skip to content
Search

C++:Type

lastmod 2018-08-17created 2017-02-072 edits

Objects, references, functions including function template specializations, and expressions have a property called type, which both restricts the operations that are permitted for those entities and provides semantic meaning to the otherwise generic sequences of bits.

Reference type

Assembly result

To check how a reference type actually works, use code like the following.

int main()
{
    int a = 11;
    int * b = &a;
    int & c = a;
    return 0;
}

Checking the asm code with objdump gives the following.

_main:
       0:   55  pushq   %rbp
       1:   48 89 e5    movq    %rsp, %rbp
       4:   31 c0   xorl    %eax, %eax
       6:   48 8d 4d f8     leaq    -8(%rbp), %rcx
       a:   c7 45 fc 00 00 00 00    movl    $0, -4(%rbp)
      11:   c7 45 f8 0b 00 00 00    movl    $11, -8(%rbp)
      18:   48 89 4d f0     movq    %rcx, -16(%rbp)
      1c:   48 89 4d e8     movq    %rcx, -24(%rbp)
      20:   5d  popq    %rbp
      21:   c3  retq

Since line 18 (the pointer type) and line 1c (the reference type) are identical, we can see that in the actual assembly code a Reference and a Pointer behave the same way.

Favorite site