C ++ primer plus Study Notes

Source: Internet
Author: User

1. The H file must be wrapped in # ifndef-# endif. The using namespace STD statement must be started with. cpp;
2. The first letter of the class name is capitalized. The member variable should start with M;
3. If the class member function does not change the value of the member variable, it should be declared as a const function;
4. When defining an object, it is best to use classa A (...); instead of classa A = classa (...), it is much easier to create temporary variables;
5. Generally, only the function whose return value is a pointer will add const to the return value. Otherwise, it makes no sense to add it;
6. A major function of the youyuan function is to reverse the order of the operands when the operators are overloaded;
7. const can only be used to modify the entire function;
8. When the function body is implemented in the class declaration, the member function automatically becomes an inline function;
9. During custom namespace, the content in class. h and Class. cpp should be included in namespace, and using namespace yournamespace is only used in Main. cpp;
10. If = is overloaded, + = is not allowed;
11. static member in class generally cannot be initialized in class declaration, but is initialized separately in. cpp, And the type must be specified, such as "int myclass: Count = 0 ;". However, if static member is a const Int or const Enum, it can be initialized in. h. Static member function is added to the declaration. Do not add static to the definition;
12. the copy constructor and the overload value assignment operator must be both required. The content of the overload = function includes the copy constructor, but more actions and return values of this judgment and delete left value new are required. Refer to stringbad. cpp.
13. the const type and reference type member Assignment Method in the const type in the const Type constructor: the const type and reference type members cannot be assigned within the const type, but can only be initialized using the member initializer list, that is, the () following the constructor. This method is more efficient for other types of members, but it is more efficient if the member itself is a class object. In addition, for a derived class, you must use this method to initialize the base class object.
14. Tips for avoiding unreasonable class assignment: Define the copy constructor & Value assignment reload function as private and the content is blank.
15. The header file is directly copied to CPP during compilation, so the include defined by the structure is not required;
16. When the function is specified as virtual in inheritance, virtual only appears in. H, not in CPP;
17. template format:
Stack. h: (the template definition is also in. H)
Template <class T>
Class Stack {
......
};
 
Template <class T>
Stack <t>: Stack ()
{
}
......
 
Usestack. cpp:
Int main ()
{
Stack <int> st;
}
 
18. Exception format:
Double hmean () [throw (const char *, double)] // [] is optional, in throw, it indicates which type exceptions will be handled by throw (not including type exceptions can be handled directly by abort ).
{
......
If (...)
Throw "hello"; // throw terminates the execution of the function, but does not return the control to the calling program. Instead, it returns the function that contains the try block along the call sequence until it finds the function.
// Throw; -- if this is used here, the control is passed to the try block on the upper layer.
......
Return ...;
}
 
Int main ()
{
......
Try {
Z = hmean ();
}
Catch (const char * s) // catch (...) indicates capturing all exceptions
{
If (S = "...")
......
}
Catch (exception & Ex) // multiple catch entries can be used to cope with multiple situations. exception is a provided exception handling class.
{
...... X
}
......
}
In fact, a common practice is to define an exception class. When a problem occurs, the catch-throw class object, for example:
If (..)
{
Classa ();
Throw;
}
...
Int main ()
{
...
Catch (classa &)
{...}
}
 
19. The three rtti elements are dynamic_cast, typeid, and type_info.

20. Four safer type conversion keywords: dynamic_cast, static_cast, const_cast, and reinterpret_cast.

Common Errors:
1. constructors not allowed a return type error:
The class has forgotten the plus points -_-!
2. When a function has a default value, it can only be included in the declaration, not in the definition;
3. Do not start with a number when naming a variable;
4. The member initialization list is written in. cpp during class inheritance;

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.