C + + Object-oriented programming must be "prescription" (Welcome to add!) )

Source: Internet
Author: User

Introduction : C + + object-oriented cumbersome mechanism, have to let you in the programming frequently "sick", find mistakes, find bugs, puzzles, is your source of disease, I am not what "physicians", but also summed up the following "prescription". Note: It is inevitable that there will be memory of the omission, the knowledge is not enough, the omission of the place also need everyone to add AH. Http://blog.csdn.net/zhanxinhang



Prescription 1☞ must always understand object-oriented programming in the two roles to play, one is you, the designer of the class, one is the user, class users, your design is for users, of course, you may also be two jobs, but also the designer is the user.

Prescription 2☞ When you design a class, you place the declaration in the header file, the definition is placed in the CPP file, because the class is for the user, put the declaration in the header file, the user can be convenient to use, and the definition in the CPP file is mainly for two points, one to improve the efficiency of compilation, two to avoid users include files after the redefinition.

Prescription 3☞ after declaring a class, you must end with a semicolon, because after declaring a class, you can also add an object name to declare the instantiated object of the class, the semicolon '; ' not the end of the declaration, but the end of declaring the class object, if a single semicolon means that no object is declared.

Prescription 4☞ Avoid instantiating this class in a constructor, otherwise a recursive dead loop will occur.

Prescription 5☞ Avoid delete this class instance in the destructor, otherwise a recursive dead loop will occur.

The prescription 6☞inline member function must be defined in the header file because the inline function is inline-expanded, similar to the #define precompilation.

The class or function in the 7☞HPP header file must be defined in the HPP header file, because the essence of HPP is to have the compiler compile the contents of the HPP file only once, and then attach the target code to the CPP destination file.

Prescription 8☞HPP The global variable in the header file must be declared static otherwise the recompile will occur, and the static nature is to have the compiler define it once.

Prescription 9☞const data members must assign an initial value in the initialization list (the c++11 can also be assigned directly = number) because it is a const variable.

The prescription 10☞const member function does not allow you to modify the data members of the class. Because it is not allowed.

Prescription 11☞ The const member function can only be accessed when a pointer to a const object accesses a member function, because the pointer to the const object does not allow you to modify the value of the memory it points to, and the const member function does not modify the value of the class object data memory, and they coincide exactly with each other.

Prescription 12☞static data members need to be defined outside the class, which is not in essence a class that contains it.

Prescription 13☞static member functions do not contain this pointer because it is not part of this object and belongs to this class.

Prescription 14☞ If one of the pointer data members in a class points to the allocated memory space, it is common practice to define the destructor and release the memory space it refers to in the destructor. Because the lifecycle of the class ends, if no destructor is defined, the editor's synthesized destructor is invoked, and the synthesized destructor does not intelligently release the memory pointed to by the data member pointer, so it must release the memory it points to before the class ends. Another option is to define the member pointer as a smart pointer type.

Prescription 15☞ operator overloading it is best not to overload two of built-in types of objects, with at least one overloaded type being a class type or an enumeration type. Because overloading two of the built-in types of objects is completely unnecessary.

Prescription 16☞ assignment operator overload (operator=) is not a virtual function, because the pointer or reference function parameter in the assignment operator might be to a base class object or to a derived class object, and therefore be confusing.

Prescription 17☞ in the inheritance hierarchy, the base class should define a virtual destructor, because the Non-virtual destructor is not inherited, and the virtual destructor will be redefined when the derived class defines the destructor (that is, the virtual table pointer points to the destructor of the derived class). When using a base-class pointer to implement polymorphism, the base class pointer points to a newly-opened (new) derived class object, and because the destructor of the base class is virtual, the destructor of the derived class can be executed successfully by using the base class pointer to release (delete) The derived class object memory.

Prescription 18☞ template class or function, declared in header file, also, the definition is to be in this header file (generally the HPP header file), because the template-specific mechanism is actually compile-time using template parameters to produce a compatible instance of the object code attached to the CPP file that uses it (special), so as to achieve polymorphism, It is not possible to generate the target code beforehand to be compatible with the instance.

Prescription 19☞ You must declare or define an object of that declaration friend before declaring a friend. Because you let me make friends with him, yes, but you must let me know what he is. ^_^

A prescription 20☞ you know only bucket, nothing more than pride and complacency.


conclusion : Bitter. ...

......................................................................................................................................................................................................

Finish. Welcome to Email:zhanxinhang@gmail.com ^_^

< reprint do not forget to indicate the source: Http://blog.csdn.net/zhanxinhang, and the author: the player zhanhang>

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.