DEV Community

Calin Baenen
Calin Baenen

Posted on

C++ vocabulary for beginners.

When I first joined C++, I was confused about some terminology, but now that I've looked it up, I understand now.
I'm here to share my wisdom in the form of a compilation of phrases I've learned.

lvalue and rvalue

An lvalue, or properly cased, "LValue", stands for "Left Value". Same with "RValue"; it means "Right Value".
A left value is any bit of code that can go on the left side of an expression, and is strictly limited to only being that left-hand of the expression.
Here's an example:

int const x = 10; // `x` is an lvalue, and `10` is an rvalue.
int y = 20; // `y` lvalue, `20` rvalue.

y = y+x; // `y` is an lvalue, `y+x` is an rvalue.
// `y+x` is an rvalue because you can't do
// `int x+y = value`.
Enter fullscreen mode Exit fullscreen mode

reference, lvalue reference, and rvalue reference.

A reference is a tool that's used to pass a variable (or its value) around.
Consider the following:

void updateYear(int& y) {
    y = 2021;
}

int year = 2006;
updateYear(year);
std::cout << year; // Prints `2021`.
Enter fullscreen mode Exit fullscreen mode

They're basically similar to pointers (T* name), except they're dereferenced when applicable, and are ensured never to be a null-like value.

What's an lvalue reference? - Well, it's not hard to explain, it's what you you just witnessed.
Reference usually refers to any reference, it's not specific, but before rvalues were added to C++ (in C++11), "reference" referred to the only kind of references that existed, lvalue references.

Copy constructor.

It's exactly what it sounds like, a constructor that copies.

What does it copy? Another instance of the same type.
It creates a new instance of a type using an old instance.

Here's an example:

struct Point {
    // The following constructor is a "copy constructor"
    // because it takes an argument of type `Point`,
    // which is the same type as the class it's in.
    // (NOTE: Copying data from `p` isn't a requirement
    //        for a copy-constructor, but doing otherwise
    //        defeats the purpose of having `p`.)
    Point(Point p) {
        // I'm not using a member-initializer list to
        // be beginner friendly.
        // More people know `this.name`/`this->name` syntax.
        this->x = p.x;
        this->y = p.y;
    }

    int x = 0;
    int y = 0;
}
Enter fullscreen mode Exit fullscreen mode

Latest comments (0)