Just a little bit of effective C + + stuff.

Source: Internet
Author: User

The paper is always shallow to go to school, I know this matter to preach.

The book has not finished,, the first pass to now, feel forgotten, did not write code, always feel a lot worse, there will be a chance to each practice past.

Use const more
The const appears to the left of the asterisk, indicating that the object is constant and the right side indicates that the pointer itself is a constant

Const member function: http://blog.sina.com.cn/s/blog_4dd77fd10100lug5.html
(const objects can only access const member functions)

Const Rational operator* (const rational& RHS) const;
Returns a const By-value accept Reference-to-const

C + + Transformation:
Const_cast
Static_cast

http://blog.csdn.net/xiaobo620/article/details/7397743


widget& operator= (const widget& RHS)
{
return* this;
}

mutable

Article Three: Initialize an object before it is used

All member variables are always listed in the initial Value column.

Use local-static instead of non-local-static
http://blog.csdn.net/insistgogo/article/details/9427389

Smart pointers, similar to Python's Junk cleaner:
STD::AUTO_PTR//Allow only one object access
Std::tr1::shared_ptr
Error when manually releasing resources, recommended class < Resources for resource management timing is the time of initialization > (RAII)

Explicit


Do not persist in single statement, for RAII reason:

The time order in which a function parameter is created between C + + is based on efficiency,
std::tr1::shared_ptr<xxx> (new XXX), test ()

If the order is as follows
New
Test
shared_ptr to pass the argument.

Test error then shared_ptr naturally blew up. Resource leaks.

Try to replace Pass-by-value with Pass-by-reference-to-const

Static object Design (threat), multithreading security?

It is difficult to change without encapsulation (threat to customer code)


Object-oriented code, data to be closed as close as possible
The larger the package, the greater the amount of change in its package.

Namespace xxx{


}

Ning has non-member non-friend function replace member function


This parameter is the qualified participant of the implicit type conversion only if the parameter is listed in the parameter column.

Std::swap can be customized according to requirements


Std::vector<std::tr1::shared_ptr<windows>> VPW (Use type safe container)

Try to avoid dynamic_casts transformation

A little bit of effective C + + stuff

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.