const int* ptr; // can’t modify the value being pointed to
int* const ptr; // can’t modify the pointer's address
const int* const ptr; // can’t modify either the value or the address
class AAA {
public:
int func();
int func() const;
private:
int a;
int b;
};
```cpp
const int& ref = 30;
```int Add(const int& num1, const int& num2) {
return num1 + num2;
}
Add(3, 4);
Example:
class AAA {
private:
int a;
int b;
};
A member initializer declares and defines member variables at once, unlike assignment in the constructor body.
class AAA {
public:
AAA() : a(0), b(10) {}
private:
int a;
int b;
};
Objects in an array are initialized using the default constructor.
AAA(const AAA& other) {
a = other.a;
b = other.b;
}
AAA aaa1;
AAA aaa2 = aaa1; // Implicitly converted to AAA aaa2(aaa1);
explicit AAA(const AAA& other) { ... }
During assignment:
AAA aaa1 = aaa2;
When passing arguments:
AAA func(AAA aaa1) { ... }
On return:
return aaa2;
RVO (Return Value Optimization) and NRVO (Named Return Value Optimization) minimize temporary object creation.
AAA(); // Temporary object created and destroyed immediately.
AAA().func(); // Temporary object created, func() called, then destroyed.
const AAA& aaa = AAA(); // Lifetime of temporary object extends as long as aaa exists.
class AAA {
public:
virtual void Func1() { ... }
virtual void Func2() { ... }
};
class BBB : public AAA {
public:
void Func1() override { ... }
};

final is called directly by its address, bypassing the vtable.Member functions have higher priority.
// Member function operator overloading
class AAA {
public:
AAA operator+(const AAA& other) { ... }
};
// Global function operator overloading
AAA operator+(const AAA& operand1, const AAA& operand2) { ... }
// Copy Constructor
AAA aaa1;
AAA aaa2 = aaa1;
// Assignment Operator
AAA aaa1;
AAA aaa2;
aaa2 = aaa1;
static_castPerforms compile-time type checking and conversion.
Examples:
dynamic_castPerforms runtime type checking and conversion.
Examples:
reinterpret_castInterprets the bits of an address as another type without type checking.