Valid C ++ class and function design and declarative Objective C ++ class and function design and Declaration

Source: Internet
Author: User
Design and declaration of Objective C ++ classes and functions

In the course of reading Objective C ++, I have been sighing for countless times. This is a good fucking write. Therefore, I decided to write the content of this book into five blogs based on my own understanding. I think whether or not you understand these terms is worth writing down. The following indexes correspond to the chapters in the book.

18: strive to make the interface perfect and minimal

19: distinguish between member functions, non-member functions, and friend functions.

20: avoid putting data member in public interfaces

21: Try to use const

22: Try to use pass-by-refernece, and use less pass-by-value.

 

18: strive to make the interface perfect and minimal

For the convenience of client calls, many methods may be defined in the interface, and many methods on the right may be redundant or repeated, which leads to too many methods in the interface, users are lost in a bunch of methods, and large interfaces are not easy to maintain. A long class definition leads to a long header file, which increases the Compilation Time. However, you do not have to worry too much about the number of methods. if you add a member function, it will make the class easier to use and add a member function to reduce client errors, that's why these methods are part of the interface.

19: distinguish between member functions, non-member functions, and friend functions.

The member function can be a virtual function but not a non-member function. If a function must be dynamically bound, it must be a virtual function and a memberfunction, the virtual function can realize dynamic binding because the subclass can rewrite the virtual method of the parent class based on its own needs to implement dynamic binding, and the non-member function cannot be overwritten. The friend function is independent from the class. It can only access the private members of the class. If a method does not need to access a private member of the class, this method should not be called the friend function of this class.

 Class  Rational {  Public  : Rational (  Int Numerator = 0 , Int Denominator = 1  );  Int Numerator () Const ;  Int Denominator () Const  ;  Const Rational Operator *( Const Rational & RHs) Const  ;  Private  :...}; 

 

The above class indicates a score. The method for adding, subtracting, multiplication, and division of scores is not provided. What method should we implement these operations, is it member function, non-member function, or friend function?

The first instinct is that these operations belong to rational and should be member functions. Then we will add a public member function for multiplication, which is like the following:

Const rational operator * (const rational & RHs) const;

Let's briefly explain why it looks like this. First, three const are interpreted. The first const indicates that the return value of the method is const, which means that we are prohibited from assigning values to a multiplication, for example, disabling a * B = 3; the second const indicates that the value of any member in RHS cannot be modified in this method, and the third const indicates that this method is the const method, the data member of the object that calls this method cannot be modified in this method. Const has many other functions that will be introduced in the next clause.

Why is the returned value by value? First, we must use a variable to store the result value of multiplication. We cannot construct a local variable in the method and return its reference, because after this method is executed, local variables are automatically recycled.

Why is the parameter of reference type? In one sentence, we try to replace by value with by reference. Clause 22 specifically addresses this issue. There is no other way to return this returned value by value. You must use a variable to store the result value. With this method, we can perform the multiplication operation.

Rational onehalf (1, 2), twofive (2, 5 );

Rational result = onehalf * twofive; // No problem

Result = onehalf * 3; // No problem. When the types do not match, the compiler will always look for the implicit type conversion method until the error is not found, since both parameters of the constructor have default values, implicit type conversion can occur. 3 is equivalent to rational (), so no problem occurs.

The exchange law of multiplication tells us: a * B = B * A; so I think onehalf * 3 can be written as 3 * onehalf, but I'm sorry,

3 * onehalf is equivalent to 3. operator (onehalf), in which 3 is the operation object, no type conversion will occur, and onehalf is the parameter, so the compiler looks for converting rational to int (assuming 3 is the int type) of course, there is no such method. It is impossible to use the member function to implement any operation of the rational and INT types. Therefore, the non-member function is used. Therefore, the multiplication method is as follows:

 
ConstRationalOperator*(ConstRational & LHS,ConstRational &RHS ){ReturnRational (LHS. numerator () * RHS. numerator (), LHS. denominator ()*RHS. denominator ());}

 

Of course, non-member functions do not need to be modified using cosnt. The multiplication result is stored in an anonymous variable. If the compilation is optimized, the result will directly exist in the variable that receives the method. With this method, there is no problem with 3 * onehalf. Now 3 is implicitly converted to rational (3, 1), and no problem will occur. If you do not want to see implicit type conversion, add the explicit it before the constructor. The last thing to consider is whether you need to call this method rational's friend function. Of course, this is not necessary, because it does not help to call it a friend function, is it necessary for another friend who does not have any help?

 

20: avoid putting data member in public interfaces

Set Data member to private and use member function to perform read and write operations. If you know that you have used object-oriented languages, you will not be confused. If these methods only return data member, these methods can be called inline, which can save the performance loss caused by method calls.

 

21: Try to use const

I also mentioned this article.

The Return Value of the const modifier method indicates that this method cannot be assigned directly;

The const modifier indicates that data member cannot be modified in this method;

The const modifier parameter indicates that this parameter cannot be modified in this method;

Different constants can also be used to overload methods. Constant objects can only call corresponding constant methods. Constant methods can be called for non-constant objects.

 

22: Try to use pass-by-refernece, and use less pass-by-value.

First, let's look at a class and then compare the two methods. Other nonsense is explained, because the default reference type of C # Is pass-by-reference, the default value of C ++ for any type is pass-by-value.

 

 Class  Dataitem {  Public : Dataitem () {cout < "  Constructor dataitem  " < Endl ;} ~ Dataitem () {cout < "  ~ Destructor dataitem  " < Endl;} dataitem (  Const Dataitem & ITEM) {cout <"  Constructor dataitem  " < Endl; Value = Item. value; text = Item. text;  //  * This = item;  //  This sentence serves the same purpose as the preceding two statements, but it will call operator =, so there will be an additional method call.  }  Const Dataitem & Operator = (Const Dataitem & ITEM) {cout < "  Operator = dataitem  " < Endl; text = Item. Text; Value = Item. value;  Return * This  ;} Dataitem * Operator &(){  Return   This  ;}  Const Dataitem * Operator &() Const  {  Return   This  ;}  Int  Getvalue (){  Return  Value ;} Void Setvalue ( Int  Val) {Value = Val ;}  String & Gettext (){  Return * Text ;}  Void Settext ( String * TXT) {text = Txt ;}  Private :  Int  Value;  String * Text ;}; 

Two Comparison methods and testsCode:

 Dataitem getdataitembyvalue (dataitem item ){  Return  Item ;}  Const Dataitem & getdataitembyreference ( Const Dataitem & ITEM ){  Return  Item ;} Void  Testdataitem () {dataitem item; cout < "  Getdataitembyvalue start:  " < Endl; getdataitembyvalue (item); cout < "  Getdataitembyvalue end  " < Endl; cout < Endl; cout < " Getdataitembyreference start:  " < Endl; getdataitembyreference (item); cout < "  Getdataitembyreference end  " < Endl ;} 

Result:

From the results, we can see that the pass-by-value method calls the constructor twice and the Destructor twice, as well as the structure and structure of the object's data members. The loss is indeed very high.

I really don't want to talk nonsense. I am so tired of writing such a blog. I have the idea that I don't want to write. I 'd like to put my time in practice.

Valid tive C ++ is temporarily suspended.

 

Valid C ++ series:

Objective C ++ constructor destructor assignment operator

Design and declaration of Objective C ++ classes and functions

Objective C ++ object-oriented and inheritance

Author: Chen taihan

Blog: http://www.cnblogs.com/hlxs/

 

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.