The default generated special function

Configuration, copy, destructor

C ++ 98, when you create a class when the compiler will automatically generate a default constructor, copy constructor, assignment function and copy destructor:

class Foo {
public:
    // 构造函数
    Foo();
    // 析构函数
    ~Foo();
    // 拷贝构造函数
    Foo(const Foo& rhs);
    // 拷贝赋值函数
    Foo& operator=(const Foo& rhs);
};

mobile

C ++ 11 introduces a new move semantics, whereby a special default function compiler generated multi-constructor and a mobile mobile assignment functions:

class Foo {
public:
    // 移动构造函数
    Foo(Foo&& rhs);
    // 移动赋值函数
    Foo& operator=(Foo&& rhs);
};

Delete special functions generated by default

In C ++ 98 can declare a function privateand does not provide functions for:

class Foo {
private:
    // 删除拷贝语义
    Foo(const Foo& rhs);
    Foo& operator=(const Foo& rhs);
};

C ++ 11 provides a clearer syntax, declare a function deleteyou can:

class Foo {
public:
    // 删除拷贝语义
    Foo(const Foo& rhs) = delete;
    Foo& operator=(const Foo& rhs) = delete;
};

Destructor better not throw an exception

Since RAII feature of C ++, when an exception is thrown away from the object when the current scope scope will do destruction operation, this time if the destructor object that will be re-thrown abnormal program termination. It is proposed in the C ++ destructor 98 is preferably set to not throw an exception:

class Foo {
public:
    ~Foo() throw();
};

In C ++ 11, to the best practices in order to achieve the default syntax of the language, is the class destructor is automatic noexcept, and even the user's own destructor is implicitly defined noexceptattributes :

class Foo {
public:
    // 等价于~Foo() noexcept;
    ~Foo();
};

Incidentally, according to the «effective modern c ++» say, though throw(), and noexceptsemantics are the same, but the use of noexceptthe function can be better optimized compiler , it is used in the C ++ 11 noexceptbetter.

Guess you like

Origin www.cnblogs.com/HachikoT/p/12112282.html