Terminology in C + +

Source: Internet
Author: User

1 declarative: The so-called declarative is to tell the compiler the name and type of something, but ignore the details. The following are the declarative expressions:

extern int x;                 // remember: The declaration of the variable plus the extern keyword std::size_t numdigits (int numbers);  // Function-Declarative class Widgets;                  // class declarative template<typename t>   // Templates (template) declarative class Graphnode;       

The declaration of each function reveals its signature, which is the parameter and return type. The signature of a function is equivalent to the type of the function. The signature of the Numdigits function is std::size_t (int), which means "this function obtains an int and returns a std::size_t".

2 Definition: The task of defining a definition is to provide 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 class or class template, the definitions list their members.

3 initialization is the process of giving the object initial value. For objects of a user-defined type, initialization is performed by the constructor. The so-called 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.

If constructors are declared as explicit, this prevents them from being used to perform implicit type conversions, but they can still be used for display type conversions.

void // function that accepts an object of type B B bObj1;  // an object of type B dosomething (bObj1);  // no problem, pass a B to the DoSomething function B bObj2 (dosomething  ); // error, dosomething should accept a B instead of an int, and there is no implicit conversion between int and b dosomething (b ());  // no problem, use the constructor of B to explicitly convert an int

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).

4 The copy constructor is used to " initialize the Self object with the same type of object", and the copy assignment operator is used to " copy its values from another identical object to the Self object";

classwidget{ Public: Widgets ();//default constructorWidgets (ConstWidget &AMP;RHS);//copy Constructorwidget&operator=(Constwidget& RHS);//Copy assignment operator};  Widget W1; //Call the default constructorWidget W2 (W1);//Call the copy constructorW1=W2;//invoking the Copy assignment operator

Be careful when you see the assignment symbol, because the "=" syntax can also be used to invoke the copy constructor :

Widget w3=w2;//Call copy constructor

Fortunately, the copy construct differs from the simple copy assignment. If a new object is defined (for example, W3 in the above statement), there must be a constructor called and the assignment operation cannot be called . If no new object is defined (such as the "W1=W2" statement above), no constructor is called, then of course the assignment operation is called .

The copy constructor is a particularly important function, because it defines how an object is passed by value (passed in the values). If you do Pass-by-reference-to-const (reference), you often do not need to call the copy constructor.

Terminology in C + +

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.