Chapter 14: Inheritance & Composition Y is inherited from X then all private elements of X are still private in Y all of the public members in the base class will be private in the derived class if the base class were not preceded by public constructors of base-class objects and member objects must be [...]
Chapter 13: Dynamic Object Creation 用new来创建对象有两个过程,分配内存,调用构造函数,而malloc仅仅是简单的分配内存。 delete只能用来释放用new创建的对象,用它释放malloc生成的对象,其行为是不可预测的。 建议在delete一个指针后把指针赋值为0,可以避免重复的释放。 当一个函数被声明成为某个类的friend之后,对它进行内联定义并不会改变他的友元状态和它是一个全局函数的事实。 当你的程序里面出现delete void* 的时候,很可能是程序出现了bug。 要创建对象的数组,要求对象类中定义了default constructor(不含参数的构造函数)。 aggregate initialization的情况除外。 delete []pointerToArray 删除一个对象数组需要在指针前加[] 定义一个常数指针可以让它表现得像一个数组名: type* const p 用new创建对象,当内存空间不够时,会调用new-handler函数。 引用new.h,你可以用set_new_handler()来设置自定义的new-handler函数 new-handler函数必须没有参数,且返回void 重载new操作之后,要使用new-handler必须在重载的函数里面调用,默认不会使用new-handler 可以重载new和delete,但是构造函数和析构函数的调用是编译器自动实现,不可改变的。 重载new的理由通常是为了更高的效率 重载的new操作必须带一个size_t类型的参数,这个参数在调用new时由编译器自动生成和传递,其值等于要生成对象的大小。 必须返回一个指针void*,因为在没有调用构造函数之前,new的工作仅仅是开辟一片内存而已 delete操作需要一个void*参数 重载new和delete的时候不要用cin,cout输出,因为iostream会调用new来创建对象,可能会死循环 在重载一个类的new和delete操作的时候,你不必显示的声明它们为static,但事实上它们是static成员。 在类继承的时候,基类重载的new和delete不会自动传递给子类。 重载new[]和delete[],用于对于对象数组的创建和删除 如果new失败,构造函数将不会执行 new操作可以带多个参数,第一个参数始终是size_t,比如: X* xp = new(a) X;
Chapter 12: Operator Overloading All the operator used in expressions that contain only built-in data types cannot be overloaded, operators can only be overloaded when a expression contains user-defined types when define a overloaded operator function, the number of arguments depends on : whether it is a unary operator or a binary operator whether it [...]
Chapter 11: References & the Copy-Constructor Reference C++中void*指针赋值给其他类型指针必须显示转换 Reference Rules: must be initialized when it is created Once initialized, it cannot be changed to refer another object You cannot have NULL references when passing argument, const reference is the first choice Copy-Constructor : Type(const Type&) it is essential to control passing and returning of user-defined [...]
Chapter 10: Name Control static has 2 meanings: allocate once at a fixed address, static storage. Local to a particular translation unit, file static. DO NOT call exit() in destructor. In C++, the constructor for a global static object is called before main(), destructor is executed after main(). external linkage: name is visible throughout all [...]