Highly effective effective C + + 55 individual study Note one

Source: Internet
Author: User

Clause 1:c++ is a language system


The C + + language as a mature and complete language system, rather than simply the extension and extension of C. Because of its wide range of features, such as object-oriented, template, standard library,

Generic programming, and so on. object-oriented C + +. Template C + + STL containers, iterators,algorithms


Clause 2: Try to replace the # define keyword with const,enum,inline


This allows us to replace the precompiled compiler with a compiler. Because the pre-compiler is replaced before compiling, there is no markup and no debugging traces are used. (And I think

Nor does it take advantage of the encapsulation and constraints of object-oriented classes. Const can define restriction constants and methods, and there are more secure and appropriate control permissions for methods and variables. Enum can

To define the shaping constants used by some classes and objects. Security, and encapsulation is better. Inline inline functions are also designed to improve program efficiency by exchanging space for time and

The process of the function into the stack, making the efficiency further improved. Note, however, that using short and small functions as inline functions is generally recommended within 10 lines

(recursion is removed). Like what:

Enum
{
max_path_size=1024,
MAX_BUFF_SIZE=1024*10,
};
inline bool Cbase64crpyt::isbase64 (unsigned char c)
{
Return (Isalnum (c) | | (c = = ' + ') | | (c = = '/'));
}

Clause 3: Use const whenever possible.

Why do you say that, because this keyword tells the compiler to strictly constrain it. Great for protecting those methods and properties, whether pointers, files, functions. Like what:
static const TCHAR * Const MUTEX_FLAG;
static const string Base64_chars;
String Base64decode (String const& en_string);

Article 4: Before the applicable object is determined to have been initialized

The methods of initializing objects in C + + are primarily done in constructors. There are two ways in which the constructor is initialized internally (in this case, the built-in type), and the list of members is initialized.
Constructor ()
{
Int i=0x0;//built-in type initialization
}
Constructor (int iIndex): M (iIndex)//initialization
The compiler calls the default constructor, initializes the list, and then calls the user-defined constructor.
{
m=iindex;//This is not called initialization, called assignment.
}
There is also a C + + initialization order for non-local static objects of different compilation units, which is not explicitly defined.
This problem is resolved by returning a reference to an object using a local function.
FileSystem & TFS ()
{
STATUC FileSystem FS;
return FS;
}

Article 5: Learn about the functions that C + + silently writes and calls


Class Empty ()
{
Public:
Empty () {};//default constructor
Empty (const empty&em) {};//copy constructor
~empty () {};//destructor
empty& operator= (const empty& EM) {};//copy assignment operator
}

The compiler can automatically generate all of the above functions by default to the class you define, without declaring it by default.
So you have to understand when you need to create custom constructors, copy constructors, and so on.


Article 6: If you do not want to use the compiler automatically generated functions, then you should explicitly deny


By default, when the user does not implement the copy constructor and copy assignment themselves (that is, overload replication). The compiler builds itself, and at some point we need our

When the object is unique, it must be forbidden to copy it. The best way to do this is to define these functions, which are private functions and do not implement them.

In this way, we can safely protect our objects.



Article 7: Declaring a virtual destructor for a polymorphic base class

C + + explicitly states that when a derived class object is deleted through a base pointer, and the base class carries a non-virtual destructor, the result is undefined.

The actual execution is when the derived component of the object has not been destroyed. Since it is undefined, it is unknown and dangerous. So remember that the base class with polymorphism should give an analysis

A constructor declaration is called virtual, and on the other hand, if it is not polymorphic, then do not declare it as a virtual destructor.


Article 8: Don't let exceptions escape destructors


If you write exception handling in the destructor, you must remember not to let it escape. That is, it has to be inside the destructor itself.

Exception-catching, handling. Failure can not allow the exception to spread, out of destructors. In addition, if the customer requests to react to an exception, it is recommended to provide an interface function,

For its capture, call processing.


Article 9: Never call the virtual function in the construction and destruction process

Failure to comply with this clause will also pose a significant risk. Also, you are a Java or C # Programmer, you have to pay more attention to this problem. For example, you call a virtual function in a constructor of a base class,

When a subclass is defined, the subclass must first call the constructor of the base class to construct it. The virtual function is then called at the time of construction, when the base class does not fully initialize the object.

To invoke the polymorphic virtual function, the result is very dangerous. and the compiler resolves it to the base class virtual function. In the same vein, do not call on destructors. Because of this kind of virtual function call,

Never descend to subclasses.


clause 10: Make operator= return a reference *this

This operator is not strange, when you need to use the = number for object assignment, then you let the function return a pointer to the object's this object. Just return the object reference =,+=

You'd better follow this standard, so you can also achieve chain assignment. It's a good habit and a rigorous coding style.



Highly effective effective C + + 55 individual study Note one

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.