Notes for writing a class

Source: Internet
Author: User

Notes for writing a class

When writing classes, we should grasp the details well, not only avoid some obvious errors, but also how to form a good programming style. Next, we will use the following example to compile the analysis class:

Class Complex

{

Public:

Complex (double real, double imaginary = 0): _ real (real), _ imaginary (imaginary ){}

Void operator + (Complex other)

{

_ Real = _ real + other. _ real;

_ Imaginary = _ imaginary + other. _ imaginary;

}

Void operator <(ostream OS)

{

OS <"(" <_ real <"," <_ imaginary <")";

}

Complex operator ++ ()

{

++ _ Real;

Return * this;

}

Complex operator ++ (int)

{
Complex temp = * this;

++ _ Real;

Return temp;

}

Private:

Double _ real, _ imaginary;

}

1. Be careful about the implicit temporary objects generated in implicit type conversion. A good way to avoid this problem is to convert the displayed type in the constructor as much as possible, and avoid writing type conversion operators.

Because the second parameter of the constructor in the above class has a default value, this function can also be used as a single-parameter constructor, this also means that implicit conversion from the double type to the Complex type can be performed. Note that this type conversion may not always be expected.

2. When passing a parameter object, we should first select the const & method instead of the value passing method. For example:

Void operator + (Complex other) will generate temporary objects when passing parameters, and the execution efficiency will be reduced.

3. We should first select "a op = B", instead of "a = a op B" (here op represents an operator ). This method is clearer and more efficient.

Why is operator ++ = more efficient? The reason is that this operator performs operations on the objects on the left and returns a reference rather than a temporary object. Operator + must return a temporary object. For example:

T & T: operator + = (const T & other)

{

//...

Return * this;

}

Const T operator + (const T & a, const T & B)

{

T temp ();

Temp + = B;

Return temp;

}

Note the relationship between operators + and + =. The former is implemented through the latter. This is not only to make the code concise, but also to ensure code consistency. Note: To prevent programmers from writing expressions like "a + B = c", the type of the returned temporary object should be "const Complex" instead of "Complex ), this is why const T is returned in the above operator +.

4. The C ++ standard stipulates that operators =, (), [], and-> must be defined as member functions, the operator functions defined in the class, such as new, new [], delete, and delete [], must be static member functions. For other operator functions: IF operator> or operator <is used for convection I/O, or if type conversion is required for its left parameter; or, if it can be implemented through the public interface of the class, this function is defined as a non-member function (in the first two cases, it can also be defined as a friend function if needed ). If operator functions need to implement virtual functions, add a virtual member function to provide virtual functions. Then, add a virtual member function to provide virtual functions, use this virtual member function to implement the operator function. Otherwise, operator functions are defined as member functions.

5. operator <should not be defined as a member function. In addition, the non-member function operator < <应该使用成员函数(通常是虚函数)来实现,并且这个成员函数的功能就是进行流输出。更进一步,operator<<应该返回一个“ostream&”类型的应用,并且所应用的就是这个流对象,这是为了实现链式操作。< p>

6. The pre-increment operator should return a very large number of references, which not only enables the client code to be written in a more intuitive way, but also avoids unnecessary inefficiency. A constant value should be returned in the Post-incrementing operator function. This method can avoid modifying the returned object, thus avoiding the problematic code like "a ++. To maintain code consistency, we should use pre-increment to achieve post-increment.

7. Avoid using reserved names. Some identifiers with leading underscores are retained in the implementation of the C ++ standard library, which are hard to remember. Therefore, it is best not to use leading underscores at all.

After the above analysis, the modified class is:

Class Complex

{

Public:

Explicit Complex (double real, double imaginary = 0): _ real (real), _ imaginary (imaginary ){}

Complex & operator ++ = (const Complex & other)

{

_ Real + = other. _ real;

_ Imaginary + = other. _ imaginary;

Return * this;

}

Complex & operator ++ ()

{

++ _ Real;

Return * this;

}

Const Complex operator ++ (int)

{
Complex temp (* this );

++ _ Real;

Return temp;

}

Ostream & Print (ostream & OS) const

{

Return OS <"(" <real _ <"," <imaginary _ <")";

}

Private:

Double real _, imaginary _;

}

Const Complex operator + (const Complex & lhs, const Complex & rhs)

{

Complex ret (lhs );

Ret + = rhs;

Return ret;

}

Ostream & operator <(ostream & OS, const Complex & c)

{

Return c. Print (OS );

}

Zookeeper

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.