Class Construction, structure analysis, relationship between temporary objects and function parameters

Source: Internet
Author: User
Tags nref

Recently, a cross-Symbian, Linux, WinCE, and Windows application layer framework has been encapsulated. However, some platforms do not have STL and some basic C ++ functions, the Framework Code of each platform must be consistent. Therefore, some codes of string and STL are encapsulated. In this process, many templates, constructor, and destructor are encountered, the problem of function parameters is actually the basic problem of C ++. Here we will make a summary and use ourselves as a memorandum, it also provides a quick review for some people whose basic skills are not very solid:

1. Basic Constructor
1. Default structure:
If a default constructor is not declared for the class display, the C ++ compiler will not generate a constructor for it, one is automatically generated only when compilation is required (compilation is required .....) The generated data will not be initialized for your class member data,
2. the constructor is displayed.
Shows a declared constructor. Explicit constructor must be initialized for class members. Otherwise, the compiler will not help you with initialization because Initialization is required by the program rather than the compiler.
The display constructor gives us a chance to initialize members in the form of a member initialization list. This gives you a chance to initialize member variables of the constant type/reference type. Otherwise, this type of member functions cannot be initialized, resulting in compilation errors such:
Class cdemo
{
Cdemo (const Int & ninput, const Int & nref): m_ninput (ninput), m_nreference (nref)
{;};
Protected:
Const int m_ninput;
Int & m_nreference
}

3. Copy the constructor,
Similar to the default constructor, if a copy constructor is not declared, the compiler automatically generates a default copy constructor when compilation is required. If no compilation is required, all bitcopy operations (direct memory copy)
When copying constructor features, the first input parameter is the type of the class and is of the const T & RHS type. For example:
Class string
{
String (const string & RHs );
}

What is the result if the parameter is const string RHS? The compiler fails. a friendly reminder should be const string & RHS, because if const string RHS is used
String strrhs;
String STR (strrhs );
During STR copy construction, strrhs should use the copy constructor to initialize the row parameter RHS, Which is exactly string: string (const string RHs). This will keep calling this function, initialization fails.

Can this const be less in string (const string & RHs? No. The first day prevents the parameter from being modified. The most important thing is the compatibility with various parameters, such
Constant parameters:
If void function (string strinput) is declared in this way );

Call function ("XXX ");
Two actions (different compilers may have more actions)
A, const string tempobj = string: string ("XXX"); // generate a temporary object. The temporary object in the parameter is const.
B, const string strinput = temp;
If strinput is declared as const, the temp object of the const qualifier cannot be accepted.
Therefore, it must be declared as const,
For the built-in type parameters of the function to be compatible with the parameter type, it must be declared as Const.
For example, test_func (const int ninput );
Otherwise, test_func (3); Compilation fails strictly. Because 3 is of the const type, some compilations can allow test_func (INT ninput) and test_func (3). For cross-platform purposes, the type problem is considered in parameters.

Of course, in test_func (char * szinput), note the following:

4. Value assignment operator:

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.