随笔分类 - OOP
摘要:1.关于“多态”: (1)应用程序不必为每一个派生类编写功能调用,只需要对抽象基类进行处理即可。这一 招叫“以不变应万变”,可以大大提高程序的可复用性(这是接口设计的复用,而不是代码实现的复用)。 (2)派生类的功能可以被基类指针引用,这叫向后兼容,可以提高程序的可扩充性和可维护性。以前写的程序可以被将来写的程序调用不足为奇,但是将来写的程序可以被以前写的程序调用那可了不起。 2.以下是使...
阅读全文
摘要:[More effective c++]: The 80-20 rule states that 80 percent of a program's resources are used by about 20 percent of the code: 80 percent of the runtime is spent in approximately 20 percent of the cod...
阅读全文
摘要:栈对象的优势是在适当的时候自动生成,又在适当的时候自动销毁,不需要程序员操心;而且栈对象的创建速度一般较堆对象快,因为分配堆对象时,会调用operator new操作,operator new会采用某种内存空间搜索算法,而该搜索过程可能是很费时间的,产生栈对象则没有这么麻烦,它仅仅需要移动栈顶指针就可以了。但是要注意的是,通常栈空间容量比较小,一般是1MB~2MB,所以体积比较大的对象不适合在...
阅读全文
posted @ 2007-10-08 22:35
能巴
摘要:禁止产生堆对象 上面已经提到,你决定禁止产生某种类型的堆对象,这时你可以自己创建一个资源封装类,该类对象只能在栈中产生,这样就能在异常的情况下自动释放封装的资源。 那么怎样禁止产生堆对象了?我们已经知道,产生堆对象的唯一方法是使用new操作,如果我们禁止使用new不就行了么。再进一步,new操作执行时会调用operator new,而operator new是可以重载的。方法有了...
阅读全文
posted @ 2007-10-08 22:23
能巴
摘要:多态与泛型(generic) 多态实际上就是泛型。所谓泛型就是指我们不为特定的类型进行专门编码,而采用对不同类型进行通用编码的方式,无论是数据结果还是算法。传统的泛型是指类似以Template function的方式使参数一般化,典型的应用是C++ STL,比如List、Vector以及algorithm。而OO已能通过接口(Interface)和抽象类(Abstract Class)进行真正意义...
阅读全文
摘要:Thus when a Base1 or Derived pointer is assigned the address of a Derived class object, the virtual table being accessed is the primary virtual table vtbl__Derived. When a Base2 pointer is assigned t...
阅读全文
摘要:Better error/exception handling. 2 ways: a) return status upwards the function calling chain and have the top calling function process the status;b) not necessarily return the status but throw excepti...
阅读全文
摘要:In order to understand "delete this" : First Step------dive into "delete p" delete p 执行了哪两个步骤?delete p 是一个两步的过程:调用析构函数,然后释放内存。delete p产生的代码看上去是这样的(假设是Fred*类型的): // 原始码:delete p;if (p != NULL) { p->...
阅读全文
摘要:PRISM as soon as you remember the following five elements when you typing your each line codes, you will not make stupid mistakes: ------Performance ------Reliability [Resource or Memory Leak [where i...
阅读全文
摘要:Basically, inheritance and composition both make one object contain another object.Then when do we need to use inheritance not composition?1. Need use polymorphism mechanism by upcasting. For c++, it'...
阅读全文

浙公网安备 33010602011771号