Effective C + + 0 Guided tour (Introduction)

Source: Internet
Author: User

Effective C + + guide (Introduction)

Terminology (terminology)

A declarative (declaration) is a name and type (type) that tells the compiler something, but omits the details. The following are declarative:

extern int x;                             // Object declaration Type std::size_t numdigits (int number);        // Function-Declarative class Weight;                             // class declarative template<typename t>                      // Templates (template) declarative

The declaration of each function reveals its signature (signature), which is the parameter and return type. The signature of a function is equivalent to the type of the function.

definition is a task that provides details that are omitted by some compiler declarations. For an object, the definition is the place where the compiler is allocating memory for this object. for function or function template, the definition provides the code ontology. For a class or class template, the definitions list their members:

intX//the definition of an objectstd::size_t Numdigits (intNumber//Definition of function{//This function returns the number of its argumentsstd::size_t Digitssofar =1;  while(Number/=Ten) !=0) ++Digitssofar; returnDigitssofar;} CALSS Widget {//definition of Class Public: Widgets (); ~widgets (); ...}; Template<typename t>//Definition of template

initialization (initialization) is the process of giving the object initial value. For objects of a user-defined type, initialization is performed by the constructor.

The default constructor is a person who can be called without any arguments. Such constructors do not have parameters, or each parameter has a default value:

classA { Public: A (); //default constructor};classB { Public:    ExplicitBintx =0,BOOLb =true);//default constructor};classC { Public:    ExplicitCintx);//is not the default constructor};

The constructors for class B and C above are declared as explicit, which prevents them from being used to perform implicit type conversions (implicit type conversions), but they can still be used for display type conversions (explicit type Conversi ONS):

voidDoSomething (B bobject);//function that accepts an object of type BB bObj1; //an object of type BDoSomething (OOBJ1);//Pass a B to the DoSomething functionB BObj2 ( -);//establishes a B according to int 28, and the bool parameter of the function defaults to Truedosomething ( -);//Error! DoSomething should accept a B instead of an int, and there is no implicit conversion between int to Bdosomething (B ( -));//Use the B constructor to display the int transformation (that is, transform, cast) to a B to facilitate this call

Constructors declared as explicit are generally more popular than their non-explicit brothers because they prohibit the compiler from performing type conversions that are unintended (and often not expected).

The copy constructor is used to "initialize the self object with the same object", and the copy assignment operator is used to "copy its value from another object of the same type to the Self object":

calss Widget { Public: Widgets (); //default constructorWidgets (Constwidget& RHS);//copy Constructorwidget&operator=(Constwidget& RHS);//Copy assignment operator    ...};         Widget W1; //Call the default constructorWidget W2 (W1);//Call the copy constructorWidget W3 = W2;//call the copy constructor! 
W1 = W2;           // invoking the Copy assignment operator

To differentiate between copy construction and copy assignment:

If a new object is defined, a constructor must be called and the assignment operation cannot be invoked. If no new object is defined, no constructor is called, that is, the assignment operation is called.

Naming habits (naming conventions)

Parameter names LHS (left-hand side) and RHS (right-hand side) are used for binary operator functions such as operator== and operator*. For member functions, the left argument is represented by the this pointer, so parameters are used alone Name RHS.

The pointer to a T-type object is named PT, which means "pointer to T". such as PW = "ptr to Widget". And RW can represent the reference to Widget.

About Threads (Threading consideration)

TR1 and Boost

Effective C + + 0 Guided tour (Introduction)

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.