This section lists the difficulties or points of attention in this chapter so that I can review and consolidate or remember. I can get the 6th pdf version of C++ Primer Plus for free if I follow a private conversation.
-
Friend: Friend function, Friend class, Friend member function
-
Declaration of friend class className;) It can be placed in a public, private, or protected section, where it doesn't matter
-
To use a friend member function, use the preceding declaration to resolve circular dependencies, and place the function definition of the class of the friend member function itself after the class in which the friend function exists.
class Tv; class Remote{}; class Tv{}; //put Remote method definitions here
-
Mutual Friends must ensure that the functions declared in the preceding class are defined after the latter class declaration, and that the prototype is in the preceding
-
Common Friend Example p610
-
Nested Class Scope Feature Table p613
-
Nested classes in templates can also use template types
-
The prototype of abort () is located in the header file cstdlib, and its typical implementation is to want the standard error stream (cerr) to send a message abnormal program termination, then terminate the program, which also returns an implementation-dependent value telling the operating system that processing failed
-
Throw exceptions, use handlers to catch exceptions, use try blocks
-
C++11 discards exception specifications (used to tell users that they need to use a try block; let the compiler add code for execution-phase checking) and is not recommended
double harm(double a)throw(bad_thing); double marm(double) throw();
-
The new keyword noexcept in C++11 indicates that the function does not throw exceptions and helps the compiler optimize the code
-
When the program unwinds to a place where it can catch exceptions, the auto-stored variables in the stack are released. If the variable is a class object, the destructor will be called on that object
-
The throw statement returns control up to the first function containing a try-catch combination that can catch the corresponding exception
-
The compiler always creates a temporary copy when an exception is thrown, even if the exception specification and the catch block specify a reference
class problem{}; void super()throw(problem){ ... if(oh_no){ problem oops; throw oops; //oops is released, so a copy is created using throw problem () } } try{ super(); } catch(problem &p){ ... }
-
References have an important feature: base class references can execute derived class objects
-
If you have an exception class inheritance hierarchy, you should arrange the catch blocks like this: put the catch statement that captures the exception class at the bottom of the hierarchy first, and the catch statement that captures the base class exception at the end
class bad1{...}; class bad2:public bad1{}; class bad3:public bad2{}; void duper(){ if(oh_no)throw bad1(); if(rats)throw bad2(); if(drat)throw bad3(); } try{ duper }catch(bad3 &b){ }catch(bad2 &b){ }catch(bad1 &b){ }catch(...){ }
-
Ellipsis can be used to indicate the type of exception, thereby catching any exception
-
You can create a handler that captures objects instead of references, and when you use base class objects in a catch statement, all derived class objects will be captured, but the derived class attributes will be deprived, so the base class version of the virtual function will be used
-
C++ now lets bad_alloc exception handling new caused memory allocation problems, header file new contains bad_ A declaration of the alloc class, which is derived from the exception class public
struct Big{ double stuff[20000]; } Big* pb; try{ pb = new Big[10000]; }catch(bad_alloc& ba){ cout<<ba.what(); //std::bad_alloc exit(EXIT_FAILURE); //#include <cstdlib> }
-
The C++ standard also provides a new way to return null pointers on failure
int *pi = new(std::nothrow) int; int *pa = new(std::nowthrow) int[500]; if(pi == 0){ exit(EXIT_FAILURE); }
-
RTTI is designed to provide a standard way for programs to determine the type of object at run time. Dynamic_ The cast operator uses a pointer to a base class to generate a pointer to a derived class. Otherwise, the operator returns a 0 - null pointer. The typeid operator returns a value that points to the type of the object. Type_ The info structure stores information about a specific type.
class Grand{static void say();}; class Super:public Grand{void say();static void write();}; Grand *pg = new Grand; Grand *ps = new Superb; Super *pm = dynamic_cast<Super*>(pg); //return pm=0 pm->say(); //invalid pm->write();//invalid Super *pd = dynamic_cast<Super*>(ps); //return pd=ps; pd->say(); //Superb::say(); pd->write; //Super::write();
-
dynamic_cast can also be used for references, but there is no reference value for null pointer objects, using bad_ A cast exception denotes that it is derived from the exception class and defined in the header file typeinfo
#include <typeinfo> try{ Super &rs = dynamic_cast<Super &>(rg); }catch(bad_cast &){ ... }
-
typeid accepts two expressions whose class name and result are objects and returns a type_ Reference to info object, type_info is a class defined in the header file typeinfo that overloads ==and!= Operator. If pg is a null pointer, bad_is raised typeid exception, which is derived from the exception class and declared in the header file typeinfo
typeid(Magnificent)==typeid(*pg); cout<<typeid(*pg).name();
-
The const operator is used to change the value to const or volatile
High bar; const High* pbar = &bar; High *pb = const_cast<High *>(pbar); //pb can modify the value of bar const High bar; //If const is pointed to, it cannot be modified
-
Static_ Cast <type-name> (expression) only if type_name can be implicitly converted to the type to which expression belongs or expression can be implicitly converted to type_ Type to which name belongs
-
reinterpret_cast
struct dat{short a;short b}; long value = 0xA224B118; dat *pd= reinterpret_cast<dat *>(*value); cout<<hex<<pd->a;
-
Pointers cannot be pointed to smaller integers or floating points, function pointers cannot be converted to data pointers, and vice versa.