obtained before the point in time that you see the assignment action.A better method of Abentry constructor is to replace an assignment action with an initialized member list:This constructor is more efficient. The version based on the assignment first calls the default constructor to set the initial values for thename,theaddress and Thephones, and then immediately assigns them a new value. All of the default constructors are wasted as a result. The member initialization list avoids this proble
1. Object-oriented programming usually solves problems with explicit interfaces (function prototypes in classes) and run-time polymorphism (virtual functions and Rtti), but in the world of templates and generics programming, the importance of implicit interfaces and compile-time polymorphism is significantly increased, although explicit interfaces and runtime polymorphism still exist.2. Usually explicit interfaces are composed of function signatures, and implicit excuses are not based on functio
1. Public inheritance embodies the is-a relationship, requires the complete inheritance of the interface, and the composite embodiment has-a or "according to something to achieve" relationship.When compounding occurs between objects in the application domain (something in the world, such as people, cars, a clip, video footage, etc.), the has-a relationship is displayed when it occurs within the implementation domain (artifacts in detail, such as buffers, mutexes, find trees, etc.), Showing the r
declaring an object in a function is that the object is created only when the function is called. Note: Try not to create static objects in inline functions, possibly producing multiple replicas.1 classPrinter2 {3 Public:4 Staticprinter Theprinter ();//Watch this .5 Private:6 Printer ();7Printer (ConstprinterRHS);8 };9printertheprinter ()Ten { One StaticPrinter p; A returnp; -}Method (2): Define a counter in the class that limits the number of instance objects by counter, and throw
function. Consider implementing the classnlcomponent { Public: Virtualostream print (ostream s)Const=0; ...};classTextBlock: Publicnlcomponent {bbs.theithome.com Public: Virtualostream print (ostream s)Const; ...};classGraphic: Publicnlcomponent { Public: Virtualostream print (ostream s)Const; ...}; Inline OstreamoperatorConstnlcomponentc) { returnC.print (s);}View CodeNon-member and virtual copy constructor are similar: Write a virtual function to do the actual work, and then a non
1. Template is an effective way to save time and avoid code duplication, and when the class template is present, the compiler only has the member functions that are used, which saves space.2. Just as the two functions have code duplication, tend to pull the duplicate code out of the independent form a function, and then let the previous function call this function, function template can also do so, even the class template can take the same idea, for e
Clause 2: Replace # define with const, enum, inline as possible1. In the clause, the phrase "this clause may be replaced by the compiler rather than the preprocessor", because # define is directly replaced by the actual definition of the macro in the preprocessing phase, according to the macro example of the book definition#define Apsect_ratio 1.653The process of compiling a program is divided into several stages: preprocessing, compiling, assembling, linking and so on, when the compiler begins
. This refactoring approach reduces its reusability. In fact, the first method is more reusable than the second. Why? Obviously, it has been refined to reuse methods. This is related to the evaluation, parsing, and final execution of lambda expressions.The previous Can lambda expressions just repeat and repeat? Of course not. Deferred execution is a good way to make it work. Deferred execution saves not a value, but rather a method or step to get a value. So, every time we call "get" The method
The stored procedure p_sys_Login is defined as follows:Create procedure p_sys_Login@ ArgUserID varchar (20), -- User Name@ ArgPassword varchar (20), -- Password@ ArgResult varchar (50) OUTPUT -- logon resultAS/*......*/ The following shows how to use the most concise and effective code in C # To execute the stored procedure and return data:/// /// User Logon Verification/// /// /// Public void Login (string
in a different way: (1) the copy assignment operator of a class can be declared to be passed by value . (2) passing by value copies the values . Here's another way to do this:1widget Widget::operator= (Widget RHS)//RHS is a copy of the object2 3{//passed In-note pass by Val4 5Swap (RHS);//swap *this ' s data with6 7 //The copy ' s8 9 return* This;Ten One}From a personal point of view, I am concerned that this approach sacrifices the clarity of the code for smart implementations, but it is true
} - -widgetoperator=(intRhs//it applies even if the - +{//operator ' s parameter type - +...//is unconventional A at return* This; - - } - - ... - in};This is just a convention, and code that does not follow this convention can be compiled. However, all types in the built-in type and standard libraries (like STRING,VECTOR,COMPLEX,TR1::SHARED_PTR, etc.) are subject to this convention in all categories (or forthcoming types, see ITEM54). So unless you have a better reason, please abide by
declare functions extensively.The compiler is not responsible for exception specifications! Give a warning, and the rest is the run-time thing.When the runtime throws an exception that is not in the exception specifications, the unexpected function is called, and one of the recommended practices in clause 14 is to use set_unexpected redefine unexpected to throw a custom "unknown exception" And join this thing in all the exception specifications. But the set_unexpected of the pro-test VS2015 wil
exception, assuming that a function called by the destructor might throw an exception, the destructor should catch the exception, and then swallow them (not propagate) or end program 2: Suppose the client needs to react to an exception thrown during the execution of an operation function, Then class should provide a normal function (not a destructor) to perform the operation. */1: Destructors should never spit out exceptions. Suppose a function called by a destructor might throw an exception,
The stored procedure p_sys_Login is defined as follows:
Create procedure p_sys_Login@ ArgUserID varchar (20), -- User Name@ ArgPassword varchar (20), -- Password@ ArgResult varchar (50) OUTPUT -- logon resultAS/*......*/The following shows how to use the most concise and effective code in C # To execute the stored procedure and return data:/// /// User Logon Verification/// /// /// Public void Login (s
The stored procedure p_sys_login is defined as follows:
Create procedure p_sys_login
@ Arguserid varchar (20), -- User Name@ Argpassword varchar (20), -- Password@ Argresult varchar (50) Output -- logon resultAs
/*
Write the specific content yourself*/The following shows how to use the most concise and effective code in C # To execute the stored procedure and return data:
/// /// User Logon Verification///
When copying objects, do not forget each ingredientThe author of this section reminds us that in the case of multiple inheritance of the copy or copy assignment operator=, it is important to consider the initialization of the base class partial data.Compare the code:Class cutsomer{...Private:stringNamestringTelphone;}; Class Prioritycustomer: Publiccutsomer{ Public:Prioritycustomer() {cout"Prioritycustomer Ctor"Constprioritycustomer RHS):p riority (rhs.priority) {cout"Prioritycustomer Copy Ctor"
to a member function or member variable that has a low level of access, so the latter's actual level of access will be increased as the former.Correction 1: Adding a const to the return typeClass Rectangle{public://...const Point upperleft () const {return PDATA->ULHC;} const point lowerright () const {return PDATA->LRHC;} //...};Resolution: After adding a const, although the client cannot necessarily, it can still lead to dangling handles: such a handles means no longer exists.Demo Sample:Clas
whether the delegate object implements the relevant method, if implemented, the call, if not implemented, do not do anything, so that the delegate object can be completely in accordance with its needs to implement the method of the Trust protocol, do not worry about Because the method is not implemented and causes problems with the program, even if the delegate object is not set, the program can run normally. Because sending a message to nil will make the value of the IF statement false.Summari
This section is mainly about why Const,inline is better than # define, summed up as follows:1 If you use # define, the compiler just foolishly replaces the contents of the define with the defined variables, takingAspect_ratio, when compiling the code, the The aspect_ratio variable is uniformly replaced by the number 1.653. there seems to be nothing wrong with this, but it can lead to potential pitfalls. . Hidden trouble One: When the error, the reason for the wrong hint is 1.653, so inconvenien
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.