Alt text:

Image that says:

HOLY SHIT!! IS THAT A MOTHERF*CKING C++ REFERENCE???

int& a = b;

  • Zagorath@aussie.zone
    link
    fedilink
    English
    arrow-up
    1
    ·
    edit-2
    7 months ago

    I’ve used C but never C++. What does it mean for a variable’s type to be int&? From when I’ve used it, & would be used to create a pointer to a variable, but the type of that pointer would be int*.

    edit:

    never mind, I looked it up. It’s a “reference” instead of a pointer. Similar, but unlike a pointer it doesn’t create a distinct variable in memory of its own.

    • xmunk@sh.itjust.works
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      7 months ago

      In my experience, it’s rare to see int& in day to day as a regular old lvalue… it essentially just allows you to alias a variable to another name. It’s much more common to see them used in function parameters to leverage pass by reference. In C++ pointers usually aren’t particularly useful compared to just passing things by reference since stack variables get auto-gc’d it’s the preferred style of frameworks like Qt and is extremely easy to use.

      Here’s a breakdown if you want more information https://en.cppreference.com/w/cpp/language/reference