Skip to main content

Some C++ features

  • explicit keyword is used to prevent implicit conversion and copy-initialization. A quick example is that class with only one parameter constructor can be used as a type conversion. The following example
class Foo {
private:
int x_;
public:
Foo(int x) : x_(x) {}
int get() const { return x_; }
};

void printFoo(Foo foo) {
cout << foo.get() << endl;
}

int main(){
printFoo(10); // might work without explicit
return 0;
};
  • virtual keyword allow polymorphism aka let child class override parent class's method.
  • cin << str的标准写法应该为getline(cin, str), 因为c++11及以前的标准中iostream里没有处理string的类方法,而这句话能成功是因为调用了string类的一个friend method,
  • inline keyword usage same as macro, but inline is type safe and can be debugged.
  • ~ is the destructor keyword, which is called when the object is destroyed. It is called when the object is out of scope, or when delete is called on a pointer to the object.
  • c++ has return value optimization and move semantics, which means that return by value (for container type) is not as expensive as it seems. The compiler will optimize the code to avoid copying the object.
  • static: static varible can be only initialized once, and be accessed by current file or function only. It is initialized when the program starts and destroyed when the program ends. It is shared by all instances of the class.
  • extern: extern is used to declare a global variable or function in another file. e.g. file1 has variable int x, file2 wants to use x, then file2 should declare extern int x; before using it.
    • sometimes we might see extern "C" void somefunction(); or extern "C++" void somefunction(); which is used to tell the compiler to use C or C++ linkage. C++ linkage is the default.
  • :: : scope resolution operator. e.g. i have a global variable x, and a local variable x, then i can use ::x to access the global variable.
  • volatile: volatile is used to tell the compiler that the variable can be changed by other programs or threads. So the compiler will not optimize the code that uses the variable.
  • mutable: mutable is used to tell the compiler that the variable can be changed by const member functions.
  • const in function declaration: const in function declaration means that the function will not change the object. e.g. int get() const { return x_; }
  • return by value: some of return by value such as std::vector will be optimized since Return Value Optimization (RVO) or Move Semantics is used. So it is not as expensive as it seems. But for some others, they be copied by calling Copy Constructor. So it is expensive. e.g. std::string will be copied.

lambda function uses [] to catch outer scope variable, it contains following cases:

  1. []: does not use outer scope variable
  2. [=]: copy all outer scope variable
  3. [&]: reference all outer scope variable
  4. [x]: copy variable x from outer scope
  5. [&x]: reference variable x
  6. [=, ...]: ... usually be the &var
  7. [&,...]: ... usually be the var decltype: short for declare type, use for look up type