C + + Primer reading notes 2 (Classic Collection) __c++
Source: Internet
Author: User
Title:Re-discussion of overloaded functions the overloaded function is a concept proposed by C + +, but not necessarily in C. For example, "1+3" and "1.0+3.0", although all are additions, do is not the same operation: the compiler to the operands due to the difference between the call of different addition operations. Except that the internal type variables can participate in the operation, there is no such a profound concept of "class" in C language. A "struct" is also just an organizing method of memory data, and does not involve processing the entire structure. Therefore, in the C language times compilers did something similar to overloading, but can be like Lei Feng, "do good things not to be remembered."
C + + has developed classes, and given "class" high expectations, the object of the class can also participate in all operations like a built-in type object. So, for the addition operation, how does the compiler know which detailed operation code to call for the addition of a class of objects? As a result, at least the operators are overloaded, even if the overloads of normal functions do not appear.
Dr. Lin Rui another reason for the need for overloaded functions in the high quality C++/C Programming Guide: Class constructors must have the same name as the class name, whereas classes often define multiple different constructors. Then it has to be overloaded.
For ordinary programmers, we can do without thinking so deeply. Overloaded functions give us at least another benefit: do not have to memorize a number of different function names, nor do you have to rack your brains to name the function. However, this book also gives a suggestion: not at all times it is necessary to overload the function, sometimes different function names can visually bring a lot of information, abuse overload is only sacrificed the information in the name.
Title:: A conceptual reference to overloaded functions: The two (can be more than two-cat-stealing) functions that appear in the same scope, if they have the same name and the formal parameter list is different, are called overloaded functions.
The first sentence of this section gives the definition of an overloaded function: an overloaded function must conform to two conditions: first, it appears in the same scope, the second is the same as the function name, and the formal parameter list is different.
One of the first conditions generally people do not think, in fact, the function name is the same and the scope of different functions exist, such as in MFC. They are completely unrelated functions.
The second condition can also be described in detail: The function name is the same, of course, this is what the function is called "overload" root. It is different from the formal parameter list, may appear in the number of formal parameters, may be expressed in different parameters, may also be expressed in the order of formal parameters.
If you want to expand, you can also cite many cases that are not overloaded functions.
First, if the same scope, the name is the same, the formal parameter list is the same, then the latter is considered a duplicate declaration of the former. --the function can be declared repeatedly because the declaration of the function does not produce the target code, but the definition of the function does not allow repetition to occur.
Two, if the same scope, the name is the same, the formal parameter list is the same, but the return value is different, then the latter is considered the wrong declaration. Functions cannot be differentiated by return values, because the function is called only by name and parameter, not by the return value. The reason for this is that the return value of the function can be discarded, and even if it is not discarded, it is not necessary to check what kind of return value I need before assigning the return value to another variable, and whether the assignment is independent of the function itself.
Third, sometimes it looks like the formal parameter list is different, actually is exactly the same, the book on page No. 229 tells four groups such examples:
Record lookup (const account &ACCT);
Record lookup (const &);//The difference is whether or not to name the formal parameter
typedef Phone TELNO;
Record lookup (const phone&);
Record lookup (const telno&);//Just alias the type
Record lookup (const phone&, const name&);
Record lookup (const phone&, const name& = "");/the difference is that the parameter is given a default value
Record lookup (Phone);
Record lookup (const Phone);//The difference is whether the const
The third group may have a false image of the number of parameters of the function, but the default parameter does not reduce the number of formal parameters. The fourth group is a bit hard to figure out: There are times when you can overload with a const, such as reference passing and pointer passing.
Title:: The organization of a file is often composed of a number of source files, which should be placed in the source file, which code can be placed in the same source file, which code must be open. This is a matter of management level.
It is a matter of management, because the organization of these codes often does not have the unique criteria. But they still have a certain regularity.
First of all, the maintenance of software is a complex system engineering. The organization of the code should be conducive to maintenance. Should try to put directly related content in the same document, irrelevant content in different documents. If the code has affinity, it will be stored in a different folder.
Second, the code of the software is a strict organizational system. Different content may be tied, or it may be necessary to have a relationship. So in the "#include" time to pay attention to the order.
Finally, and most importantly, some code can be reused (or must be reused) in the same project, and some code can only appear once in the same project. There are class declarations, function declarations, variable declarations, etc. that can be reused, such as the entity of the class, the entity of the function, the definition of the variable, and so on. So it's a good idea to put reusable content in the H file and put it in the CPP file that you can't reuse.
Take the class declaration and the entity of the class as an example, if you put all the contents of a class into the same file, there will be problems. Because the declaration of a class must be "visible" in other instances of the class, we often add a "#include" to the header of the file, and the entity of the resulting class is compiled multiple times, creating a conflict at the time of the connection.
As mentioned in the previous article, inline functions are the only functions that allow (and must) be visible at compile time for function entities. So inline functions can be placed in the H file. The C + + rule has a reference to this: a function written directly in the declaration of a class is considered an inline function.
When Visual C + + gives the files of a Class A default name, the file name is often the same as the class name. If the class name starts with "C", the file is a text other than the beginning of the word "C". such as class "CMyClass", its code is stored in the following two files: "MyClass.h" and "MyClass.cpp". The reason is VC + + recommended class name to start with C, as for why the filename does not appear in the beginning of the "C", may be out of the habit of Microsoft IT.
Title:: A constructor reference to a class: A constructor is a special member function.
Note: Constructors are indeed a class of "special" member functions. Its specificity is at least in the following aspects: one is that its invocation does not bother the programmer, as long as the class object is created it will be invoked, and it does not allow the programmer to explicitly call. The second is that they are required, and if the programmer is lazy, the compiler will automatically create a simple constructor. The third is that their names are not considered by programmers, and they are directly the same as the class names. Four is that they have no return value.
These features are detailed below:
First, they are automatically invoked when the class object is created, creating
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.