Effective C + + notes: 4 design and declaration

Source: Internet
Author: User

Last night to see before bedtime, sure enough to sleep before reading this habit to be forced to form.

Clause 18 let the interface be used correctly and not misused

Not easy to misuse, such as Date (1,2,1991)->date (Day (1), Month (2), Year (1991)); Replace object with function

Article 19 design class like design type

Creation and destruction;

Copy constructor (defines how pass by value is implemented);

The legal value (the throwing process that affects the exception);

Do you need to mate with an inheritance graph system (inheritance graph,virtual/non-virtual)?

Type conversion?

How are operators and functions designed?

Non-declared interface?

How much generalization (if you actually define the whole types should define the class template)?

Do you really need to define a new type (derived Class/non-member function can replace it)?

Terms of Prefer Pass-by-reference

Pass-by-value is a copy of the actual argument, the pass-by-reference (compiler bottom) really passes the pointer.

(1) By-value means calling copy Constructor/destructor, and if it is derived, design more. A series of constructors and destructors are called, at a high cost.

(2) Pass-by-reference avoids slicing (object cutting problem) (type conversion, derived attribute is converted to base class)

[1] Built-in objects, STL second iterators and function objects with Pass-by-value

Clause "T" try to return a reference when you must return a object

--see reference declarative should think about what the other name is.

(1) The local object of the function class is destroyed before the function exits, and reference points to the value being destroyed. (No defined behavior)

(2) Do not use local static in the function, for example:

Const RATIONA&L operator * (const rational& lhs,const rationnal& RHS) {static Rational result = LHS*RHS}

Call (a*b) = = (C*d) will always be equal to true, equivalent to calling operator== (Operator* (A, B), operator* (c,d))

Cause: The static object is created once and is not created again because the same global variable result is returned for reference,* (A, B) and * (C,D) operations, equivalent to = = (Result,result)

Terms of Declare data members Priviate

(1) Consistency of access data; it is easy to use, each call must add (), the first time to write a C + + small Project A polynomial calculator, getter (), setter () Some members have, some do not, the code becomes longer/modified every time to go back to view, trouble.

(2) Encapsulation, through function access, even if you modify this member variable in the future, the customer does not know that the class internal implementation has changed, and God does not know.

The [*] function is run-time evaluated, and the member variable is determined at compile time. Use a function when memory is tight, ask for speed with member variables (similar to brush title: Time and Space tradeoffs)

[*] encapsulation is inversely proportional to the amount of code destruction. Cancels public member, all client code that uses it will be destroyed; protected member variable derived class cup destroy

Terms of prefer non-member non-friend functions to member functions

Terms Declare non-member functions when type conversions should apply to all parameters

You can participate in an implicit conversion only if the parameter is listed in the parameter table.

If the class a{molecule A, the denominator b;opeartor * (const a& c) {return a*c.a+b*c.b;}}

result = var * 2//equivalent to the type of var.operator* (2) Parameter 2 cup converted to Var

result = 2 * var//error:2.operator* (VAR) int type no operator

Operator (cosnt a& a,cosnt a& b) {} can be

Terms of consider support for a non-throwing swap this isn't looking closely.

Effective C + + notes: 4 design and declaration

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.