Effective C + + clause 5

Source: Internet
Author: User

Understand what functions are written and called by C + + by default

This section has two points of knowledge.

First
For a class
1. If there is no constructor in class, the compiler declares a default constructor for class.
2. If class does not have a destructor, a copy constructor, and a copy assignment operator, then class declares these three functions.
3. The compiler generates them only when destructors, copy constructors, and copy assignment operators are called, so let's be clear that declarations and builds are separate things.

The following code:

//定义的classclass Empty{};//编译器声明的classclass Empty{public:    Empty(){}    Empty(constEmpty& rhs){}    Empty& operator=(constEmpty& rhs){}    ~Empty(){}};
Secondly

When there is a reference type of data or const type data in the class. The compiler refuses to automatically generate the copy assignment operator function. Unless you define it yourself.
The following code:

//对于这个类,编译器不会自动生成copy assignment template<typename T>class NamedObject{public:    NamedObject(constcharconstvalue);    NamedObject(const std::stringconstvalue);private:    std::string& nameValue;    const T objectValue;};

Effective C + + clause 5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.