less than 1 minute read

Categories:

Tags:

C++ is an international standard.

  1. Deterministic Object Lifetime ```c++ #include #include

void use_string() { { std::string s{โ€œHello Worldโ€}; // s is created here std::string s2{โ€œHello World2โ€}; // s2 is destroyed // s is destroyed }

{
    std::ofstream file("output.txt");
    file << "data";
} //file closed }
Also known as RAII(Resource Acquisition Is Initialization)

3. Destructors

```c++
struct MyType
{
    MyType() : ptr(new int(5)) {}
    ~MyType() { delete ptr; }

    int *ptr;
};

void my_type_test()
{
    MyType obj;
}//obj out of scope, destructor called
  1. Templates (STL, algorithm, containers)
template<typename First, typename Second>
struct Pair
{
    First i;
    Second j;
}

#include <vector>

void use_pair()
{
    Pair<int, double> p;
    std::vector<int> vec;
    std::vector<double> dvec;
    std::vector<Pair<int, dobule>> pvec;
}

Leave a comment