Features from c++ 98
Tags: C++
C++ is an international standard.
- 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
- 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