Objective C ++ Reading Notes (18-28): Design and declaration of classes and functions

Source: Internet
Author: User

Design and declaration of classes and functions

Let's look at the author's point of view:

How to generate and destroy an object: Constructing the reloads of destructor, new, and delete

Object initialization and assignment: Construction, copy construction, and assignment Overloading

New types of conversions: normative test of valid values, inheritance system, and implicit conversion

Access Permissions: public, protected, and private

 

Item18 -- strive to make the Interface complete and minimize

1. Kiss principle; 2. Importance of header files;

 

Item19 -- differentiate member functions, non-member functions, and friend Functions

1. To implement a virtual function, it must be a member function.
2. Set operator <and operator> to non-members. If you need to obtain the Non-Public member variable of the class, declare it as friend. The reason is that if func is member, the subsequent writing sequence should be OBJ> Cin, OBJ <cout, which does not conform to the habit.
3. Only non-member can implement type conversion on the leftmost parameter. If type conversion is required for the leftmost parameter of function F, F is non-function. If you need to obtain the Non-Public member variable of the class, it is declared as frind.
For example, operator * (Class & LHS, Class & RHs) is a 2 * obj2 call, type conversion needs to be performed on 2 (the constructors declared as explicit can block implicit type conversion), so it must be non-member

Item20 -- avoid putting data members in the public interface

There are three reasons in negative tive to explain why they should not be put in public interfaces.

    1. Consistency. All subsequent operations on class objects require (), that is, only functions can be called and variables cannot be obtained.
    2. Obtain control, such as read-only, read-write, and non-processing. It is implemented through different functions.
    3. Function abstraction provides an excuse for upper-level users not to worry about how to implement the underlying layer.

However, in actual programming, few people can do this completely. After all, they need to spend some time writing get and set. For now, I have not found a way to automatically generate get and set functions, therefore, the fish and the bear's paw cannot have both sides. If you want to get the benefits, you have to write get and set.

Item21 -- use const whenever possible

1, * in the middle, pre-set content and then set the needle
2. the return value is modified with const, indicating that the return value is read-only and cannot be modified.
3. The function is modified with const, indicating that the function cannot modify any variable. The function can be overloaded accordingly. A const function is called by a const object, and a function without a const is called by a non-const object.
What is the true meaning of const? Immutability. The specific entity has two types of statements: A, bitwise. B, conceptual, and A. If they are not modified, they are considered unchanged. B makes a judgment on the conceptual level. Even if the underlying layer is modified, the upper layer concept remains unchanged, that is, unchanged. However, the C ++ language only supports a. To cope with B, mutable modifiers are introduced to modify the underlying variables that remain unchanged in the upper layer.
4. const can cancel constants through const_cast

Item22 -- try to use pass-by-reference, and use less pass-by-Value
    1. Values are transmitted in C language.
    2. The cost of transferring values is relatively large, and the object copy structure will be called. If the class is more complex, more objects will be created and parsed.
    3. Transferring references will avoid cutting issues. Func (base &) and func (base) function declaration. The F () virtual function is called internally. If a derived object is passed, the passing reference will call derived. F (), while passing the value will cut and call base. F ()
Item23 -- when you must return an object, do not try to return a reference

Example of heavy-load Multiplication

Inline const rational operator * (const rational & LHS, const rational & RHs)

{

Return rational (LHS. N * RHS. N, LHS. D * RHS. D );

}

Value is returned. if reference is returned, no real object is referenced after internal variable analysis.

Item24 -- exercise caution when selecting between function overload and parameter default

Void g (INT x = 0 );

G ();

G (10 );

Void F (); void F (int x );

F ();

F (10 );

Be careful when selecting the two methods to avoid ambiguity

Item25 -- avoid overloading pointer and numeric types

Void F (int x );

Void F (string * PS );

F (0)

The existence of 0 will cause ambiguity between the pointer and the value, so we must resolutely avoid overloading the pointer and value.

Item26 -- defends against latent ambiguity (ambiguous) States

C ++ has a philosophical belief that potential ambiguity is not a mistake,ProgramIt is a disaster to put all the problems into operation. So programmers should avoid ambiguity.
Class conversion. One is that the copy construction method can be implicitly converted, and the other is the operator class () method. When type conversion is required, there will be ambiguity.

    1. Standard language conversion: 6.02 can be converted to int or char.
    2. This is also true for multi-inheritance.

When there is ambiguity, the programmer should explicitly explain which method to adopt.

Item27 -- if you do not want to use the member functions generated by the compiler, you should understand and reject it.
    1. Use private modifier to prevent public calls
    2. It is not defined to prevent calls such as friend.

PRIVATE:

Array & operator = (const array & RHs); (Note: this parameter is not defined)

 

Item28 -- try to cut global namespace

Namespace name1 {

}

Using namespace name1;

It is best for everyone to separate them with their own names. This can be similar to the concept of a Java package.

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.