Get the App
SLTechnology News&Howtos  ›  Servers  › 

C++ notes continued

Shulou Source: shulou.com Published: 2022-06-03 09:21:21 12月06日 Update

C++ Note 1. Int + variable initialization there are only two kinds of direct initialization: use parentheses, int a (10); copy initialization: use =, copy 10; (copy constructor of the object) 2.cin and getline read string cin read string with space as division getline (cin,line) read string, with'\ n'as partition 3. Function pointer

Bool lencom (void)

Typedef bool (* fun) (void)

Fun pf1 = lencom

The fun pf1 = & lencom; / / independent function is equivalent to using the function name directly and adding & before it.

Member functions must use &

4. Default constructor default constructor call timing: when defining an object, the default constructor is called as long as no initializer is provided. Default constructor for a class: even if there is only one constructor defined by a class, the compiler will no longer automatically generate a default constructor to customize the default constructor: the no-parameter constructor is the default constructor for the class.

Use the default constructor:

Sales_item myobj (); # error declares a function instead of defining an object

Sales_item myobj;# is correct, call the default constructor

Sales_item myobj=Sales_item (); # correct, copy assignment

5. When to use the copy constructor: when initializing a newly defined object with an object of the same type, the copy constructor is explicitly used; when an object of that type is passed to or returned from the function, the copy constructor is implicitly used; the elements in the order container are initialized; the array elements are initialized according to the element initializer list. For example, the procedure of vectorsvec (5) for the compiler first creates a temporary value to initialize the svec using the default constructor of string, and then uses the copy constructor to copy the temporary value to each element of the svec. Default copy constructor: if there is no copy constructor for the class, the compiler will always synthesize a copy constructor for the class. The default constructor for this composite behaves as a member-by-member initialization, initializing the new object as a copy of the original object, that is, keeping the non-static members exactly the same. Disable replication using private modifier copy constructor can disable replication

Tags: Functions objects elements characters strings members types compilers compilers timing notes casks identical copies variables only containers parentheses pointers arrays Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Linux vpn Huawei MariaDB