Writing the terms of a C + + class

Source: Internet
Author: User
Tags throw exception

How to define a class professionally? Here's a record of your checklist.

-Attention points on constructors

A. Constructors declared as explicit, avoiding implicit conversions

B. Consider disabling the copy constructor, assignment function

in C + +, the default for a class produces the following member function (which is usually not controllable if you do not define it)

Class empty () {        public:                empty ();                ~emtpy ();                                                   empty* operator& ();                  Accessor operator                Const empty* operator& () const;      Address operator Const        Private:                  empty (const empty &);                Assignment constructor                empty& operator= (const Empty &);     Assignment operator}   

Here the address function does not have to say, the key is the assignment related to the two functions, the default is a shallow copy, so still disabled, refer to the destructor note point

C. Constructors Although you can throw exceptions, but no return value, consider implementing an init function separately as a supplement

-Initializes a member variable with an initialization list instead of an assignment. There are two situations in which you must do this:

A. Members of the const \ Reference

B. Constructor of a base class is required in a method of a derived class

Base (int x): m_x (x) {}derived (int x): Base (x) {} 

-member function if the state of the class is not changed, the declaration is followed by a const

-Make the member variables private more controllable and transparent as possible

-The implementation of a non-member method instead of a member combination function that requires multiple objects

-Consider law of Demeter, make a trade-off appropriately, less wrapper or intermediary classes

-about the point of attention of the destructor
A . When a class is to be inherited (it often contains a virtual function to polymorphic), to provide a virtual destructor, the following conditions exist:

    Base *PB = new Derived ();    Delete pb;//the destructor for the derived class is not called here, although the virtual pointer of PB points to the derived virtual table, but there is no virtual destructor in its virtual table, which causes the derived object resource to be compromised

B. Whenever a destructor for a base class is virtual, all derived classes, regardless of whether or not they explicitly write a virtual destructor, can be considered virtual by the derived destructor. such as a virtual pointer of the same type, all derived defaults have

C. Do not throw exception in the destructor, otherwise implement this exception interface
If a class contains pointer and reference members, the type usually needs to implement the destructor (this is not necessarily virtual, but you need to consider cleaning up the memory)
Further, once the destructor is implemented, it is necessary to implement the copy constructor and copy copy function, otherwise it needs to be explicitly rejected (or, for one reason only: Making a copy makes no sense, see the first clause here)

Writing the terms of a C + + class

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.