"C + +" Digest of "effective C + +"--1

Source: Internet
Author: User

"Effective C + +" read Note 01
    • size_t is an unsigned (unsigned) type used when calculating the number of C + +. It is also a parameter type accepted by the operator[] function in Vector,deque and string.

    • Define the declarative and the definition, for the object, the definition is the place where the compiler is allocating memory for this object, and for function or function template, the definition provides the code itself.

    • The default constructor either has no arguments, or each parameter has a default value.

  class B {  public:    explicitB(int0booltrue//这也是一个default构造函数  }

Constructors that are declared as explicit can prevent them from being used to perform implicit type conversions, but can still perform display type conversions.

void dosomething(B object) {...}B oboj1;dosomething(bobj1); //ok,因为dosomething接受的是一个B类型的对象dosomething(28); //错误,dosomething接受一个B类型的对象,不是一个int, 而int和B之间没有隐式转换dosomething(B(28)); //ok,用B的构造函数将int显式转换为B类型

Because we generally do not want the compiler to perform unintended type conversions, we encourage the constructor to be declared as explicit

Copy constructor and copy assignment operator
class A {public:    A();    A(const// copy构造函数    operator= (const//copy assignment操作符    //调用copy构造函数//调用copy assignment操作符

The copy constructor is used when constructing a new object, instead of the normal assignment when constructing a new object, the copy assignment operator is called.

The copy constructor is very important for the way Pass-by-value is passed. Consider

void func(A a) {...}A a1;func(a1);

The object of type a here is Pass-by-value, so A1 is copied in, so a temporary a object is constructed, and the temporary a object is done with the copy constructor.

for custom types, however, pass-by-reference is a better choice .

C + + Digest of effective C + +--1

Related Article

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.