C++ basics

박영재·2024년 10월 7일
post-thumbnail

Const

  • const with primitive types and pointers:
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
  • Overloading with const:
class AAA {
public:
	int func();
	int func() const;
private:
	int a;
	int b;
};

Reference: Alias of Memory Space

  • A reference works like a pointer but is safer because:
    1. A reference cannot be null.
    2. A reference binds only once.
  • A reference can refer to a const value:
    Example:A temporary variable is created, and the reference becomes its alias. This is useful when passing const values as arguments.
    ```cpp
    const int& ref = 30;
    
    ```
int Add(const int& num1, const int& num2) {
	return num1 + num2;
}

Add(3, 4);

Class

Example:

class AAA {
private:
	int a;
	int b;
};

Member Initializer

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;
};

Object Array

Objects in an array are initialized using the default constructor.

Copy Constructor

  • Default Copy Constructor:
AAA(const AAA& other) {
	a = other.a;
	b = other.b;
}
  • Implicit conversion from the assignment operator to the copy constructor:
AAA aaa1;
AAA aaa2 = aaa1;  // Implicitly converted to AAA aaa2(aaa1);
  • Using the explicit keyword to prevent initialization with the assignment operator:
explicit AAA(const AAA& other) { ... }

When is the copy constructor called?

  1. During assignment:

    AAA aaa1 = aaa2;
    
  2. When passing arguments:

    AAA func(AAA aaa1) { ... }
    
  3. On return:

    return aaa2;
    

RVO (Return Value Optimization) and NRVO (Named Return Value Optimization) minimize temporary object creation.

Temporary Objects

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.

Inheritance

  • Construction of a subclass follows that of its superclass.
  • Destruction of a subclass precedes that of its superclass.
  • Objects are destroyed in reverse order of their creation.

Virtual Destructor

  • A subclass’s destructor calls the superclass’s destructor.
  • The superclass's destructor must be virtual; otherwise, only the superclass’s destructor is called when a superclass-typed object is destroyed.

Virtual Function Table

  • The virtual table is an array of function pointers.
  • An object with virtual functions or one that inherits from a class with virtual functions contains a pointer to the virtual table, which is used to look up functions.
class AAA {
public:
	virtual void Func1() { ... }
	virtual void Func2() { ... }
};

class BBB : public AAA {
public:
	void Func1() override { ... }
};

Optimization with final

  1. A method marked as final is called directly by its address, bypassing the vtable.
  2. This removes indirection and allows inlining of the method.

Operator Overloading

Two Ways to Implement Operator Overloading:

  1. As a member function
  2. As a global function

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) { ... }

Assignment Operator

  • The default assignment operator performs a shallow copy.
  • Copy Constructor vs. Assignment Operator:
// Copy Constructor
AAA aaa1;
AAA aaa2 = aaa1;

// Assignment Operator
AAA aaa1;
AAA aaa2;
aaa2 = aaa1;
  • A subclass’s assignment operator does not implicitly call the superclass’s assignment operator.

Type Conversion

static_cast

Performs compile-time type checking and conversion.

  • No runtime overhead
  • Type unsafe

Examples:

  1. Subclass ↔ Superclass
  2. Primitive type ↔ Primitive type

dynamic_cast

Performs runtime type checking and conversion.

  • Runtime overhead
  • Type safe

Examples:

  1. Subclass → Superclass
  2. Superclass → Subclass (if superclass has at least one virtual function)

reinterpret_cast

Interprets the bits of an address as another type without type checking.

profile
People live above layers of abstraction beneath which engineers reside

0개의 댓글