Thinking Again In C ++ (3) Misunderstanding of default parameters

zhaozj2021-02-16  54

Love the Thinking in series, so I played this name. The idea of ​​this article also comes to this book, or refers to comparison, or in-depth mining, or makes picking out, or has a feeling, including Thinking In C , and even including Thinking in Java.

Thinking Again In C (3) Misunderstanding of default parameters

Keywords: C , default parameters, default argument, functions, function, constructors, constructors, misunderstandings

When using the default parameters, you should pay attention to avoiding the following mistakes.

1. Abuse default parameters, damage the structure and readability of the code. Void F (Bool B = false) {if (b) {file: // code of open file} else {file: // code of close file} Open the file and close the file is not in common on the code, Two functions that belong to the same category are the same as the realization mechanism. If you are fabricating a parameter hard, you can make them in one, there is nothing benefit! On the contrary, who can remember to live F (TRUE) to open, f () represents close? Moreover, F (false), f () can close the file if the caller mixes will increase the difficulties on maintenance. In this case, write two independent functions, very clear. Void open () {file: // code of open file} void close () {file: // code of close file} It is also worth discussing. Class cstring {private: char * pcdata; public: cstract (char * pc = null);}; cstract :: cstring (char * pc) {ix (pc == null) {pcdata = new char [1]; // ...} else {pcdata = new char [strlen (pc) 1]; // ...}} This is more confusing, "is a constructor, of course, written in a piece." Some people said. Not too! It should be seen that the codeless code used with the constructor with a char * parameter is completely separated, and the default parameter value NULL does not have any effect when setting the data member. The cstring () constructor should be rewritten as follows: class cstring {private: char * pcdata; public: cstract (); cstract (char * pc);}; cstract :: cstring () {pcdata = new char [1]; // ...} cstring :: CString (char * pc) {pcdata = new char [strlen 1]; // ...} Summary: (1) Anyone who uses the default parameter value as IF judgment, and If the code is determined, the code is completely different, and it should be detached into two independent functions. (2) Only the default parameter value is treated without discrimination in the function body, that is, the function may be reasonable if the function is the same as the implementation mechanism of any parameters.

2. Multiple default parameters may introduce a logical confused call mode design, not only to provide the correct functionality of the customer code, but more importantly, the limitations of the incorrect use method can be limited. Class cpoint {public: int x; int y; cpoint (int x = 0, int y = 0) {this-> x = x; this-> y = y;}}; at first glance, there is no problem. When constructing a CPOINT object, if the initial value of x, y is not specified, set to the origin coordinates. Let's test: cpoint pnt1; cpoint PNT2 (100, 100); cpoint PNT3 (100); file: // [1] found that the value of PNT3 is (100, 0), running to the x-axis. We can't force them at both the two parameters to bind two parameters, or without default. But if you go to default parameters, the situation will improve. Class cpoint {public: int x; int y; cpoint () {x = 0; y = 0;} cpoint (int x, int y) {this-> x = x; t-> y = y;}}; This way, statement [1] will cause compilation errors to remind users. The leaf bar will say: "CPOINT PNT3 (100); initialized to the x-axis, this is what I want." Really? " So, please clearly indicate this unique call method in your class document, and tell the user, initialize the point to the Y-axis is CPOINT PNT4 (0,100); this asymmetric form. As for me, Self Document is ok. 3. Embodiments may appear simply when overloaded, casually: Void F (int A, int b = 0) {} Void F (int A) {} Although the potential ambiguity is not an error, However, once f (100); this code, the incubation period can be over.

4. The terms of the schizophrenia EFFECTIVE C 2nd in the function call are added here for this integrity. This rare condition has the condition that the derived class has rewritten the default parameter value of the base type virtual function. Class CBase {public: Virtual Void F (int i = 0) {cout << "in cbase" << i << endl;}}; class cderive: public cbase {public: virtual void f (int i = 100) { Cout << "in cderive" << i << endl;}}; cderive d; cbase * Pb = & d; pb-> f (); file: // [2] After running output: in CDerive 0 Remember, The default parameter is static binding, and the virtual function is dynamic binding, so [2] is running the CDerive :: F () function, and the default value used is CBASE 0. Exterior: How to (b) The number of visits is so low, only 1/3 of (1). I feel that (2) is better than (1), but there is no comment, the sadness ...

Thinking Again In C (2) self-assignment is non-break-up http://www.9cbs.net/develop/read_article.asp?id=16713

Thinking Again In C (1) Constant Principle http://www.9cbs.net/develop/read_article.asp?id=16554

转载请注明原文地址:https://www.9cbs.com/read-23820.html

New Post(0)