搜尋此網誌

2025年10月14日星期二

Operator Overloading

In C++, operator overloading lets you redefine how operators (like +, -, [], ==, etc.) behave when used with user-defined types (classes/structs). It’s a form of compile-time polymorphism, making objects act more like built-in types.

In C++, the return type of a function (or operator overload) specifies what kind of value the function gives back to the caller.

The caller is simply the function that initiates a call to another function. The callee is the one being executed as a result.

In C++ (and C), when we talk about the left-hand side operand (LHS operand), we usually mean the expression that appears to the left of an assignment operator (=).

In C++, a prototype usually refers to a function prototype. It’s essentially a declaration of a function that tells the compiler the function’s name, the return type, the number and types of parameters, but it does not include the function body (implementation).

std::string& item means:
"item is a reference to an existing std::string object."

alias: (computing) a name that can be used instead of the actual name for a file, internet address, etc.

int x = 10;

int& ref = x;   // ref is another name for x

ref = 20;       // changes x to 20


In C++, void is a special keyword that essentially means “no type”.

If a function doesn’t return a value, you declare it with void:

void greet() {

    std::cout << "Hello!" << std::endl;

    // no return statement needed

}

Here, greet() performs an action but doesn’t give back a result.


In C++, the expression return *this; is very common inside class member functions.

class Number {

    int value;

public:

    Number(int v=0) : value(v) {}

    Number& operator=(const Number& other) {

        value = other.value;

        return *this;   // return the current object by reference

    }

};

Microsoft Copilot

沒有留言:

發佈留言