Valid tive C ++ Item 4: determines that the object has been initialized before it is used.

Source: Internet
Author: User

 

 

Experience 1: manually initialize built-in objects because C ++ does not guarantee that they are initialized.

Example:

 

Int x = 0; // Manual initialization of int const char * text = a c-style string; // Manual initialization of the pointer double d; std :: cin> d; // The Initialization is completed by reading the input stream.

 

 

Experience 2: It is best for constructors to use the member Initial Value column (member initialization list) instead of the assignment operation in the constructor itself ). The member variables listed in the initial value column should be arranged in the same order as their declaration in class.

 

Example 1: Initialize with a value assignment

 

# Include
 
  
Using namespace std; class A {public: A () {cout <default constructor <endl ;}; A (int v): value (v ){}; A (const A & a2) {cout <copy constructor <endl;} const A & operator = (const A & lhr) const {cout <operator = <endl; return * this;} private: int value ;}; class B {public: B (A a2) {a = a2 ;}; // use the value assignment operation to initialize private: A a ;}; int main () {A a (1); B B (); // mainly through the output to see the function that defines the call of the B Variable system (pause );}
 

 

Output 1:

 

Copy constructor // call copy constructor of A to generate the a2 parameter for the B constructor.

Default constructor // before entering the constructor of B, call the default constructor of A to define.

Operator = // call the value assignment operator to assign a value to a2


Example 2: Use the member Initial Value column

 

#include 
 
  using namespace std;class A{public:A(){cout << default constructor << endl;};A(int v):value(v){};A(const A &a2){cout << copy constructor << endl;}const A& operator=(const A &lhr) const{cout << operator= << endl; return *this;}private:int value;};class B{public:B(A a2):a(a2){};private:A a;};int main(){A a(1);B b(a);system(pause);}
 

 

Output 2:

 

Copy constructor // call copy constructor of A to generate the a2 parameter for the B constructor.

Copy constructor // call copy constructor of A to copy a to a2

 

 

 


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.