Chinaunix首页 | 论坛 | 博客
  • 博客访问: 318794
  • 博文数量: 88
  • 博客积分: 2051
  • 博客等级: 大尉
  • 技术积分: 950
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-14 23:59
文章分类

全部博文(88)

文章存档

2012年(3)

2011年(2)

2010年(9)

2009年(14)

2008年(60)

我的朋友

分类: C/C++

2008-05-03 00:49:56

以下内容参考了Coding_Idioms_for_Symbian_OS_v1_0_cn.pdf
Symbian编程的处理异常的三大解决方案:
1.所有可能出现异常的函数以L结尾,以向上传递标准中的throw exception语法,这样异常总会在某个TRAP中被处理。
2.被自动变量拥有的内存块必须被push到cleanstack中,以便在出现异常时被完全释放而不出现内存泄露。记得C++中的auto_ptr吗?CleanStack有类似的功能,总能以优雅地方式释放掉你手动分配的内存。
3.还记得Effective C++里面说的原则吗?永远不要在Constractor中放置可能出现异常的代码。在Symbian中它得到极致地体现。不要在构造函数中为你包含的对象分配内存,利用两步构造技术来实现吧。NewL和NewLC就是这样的方式来构造对象的。
 
总之,在Symbian中编写代码应该非常小心,new都被重写了,还有什么比这更小心的了呢?仔细review每一行代码吧!
 

The ‘T’ type is a value type, owning no external object. T类型表示是一个基本类型,就好像int,char一样。

The ‘R’ type is a resource type; typically this is proxy for an object owned elsewhere. In practice, the 'R' type is a handle to a resource owned and managed by the Kernel. R类型表示对某种资源的封装。

This shows a typical ‘C’ class, derived from CBase, and highlights the use of two phase construction and use of the cleanup stack to implement the complete construction of an instance of that class on the heap. C类型从CBase继承而来,任何C类型都在heap上构造,需要两步构造和使用CleanupStack来帮你管理内存。

The three examples show the use of ‘M’ (abstract interface) classes, the only type of multiple inheritance used in Symbian OS.They show how interfaces can be used to define a protocol. The interface is implemented by a protocol provider, and called by a protocol user. The user is not supposed to know anything about the provider's implementation, only about the protocol it's interested in. M类型定义了一个抽象接口,Symbian中的多重继承都会看到M类型的影子。相当于给Derived Class定义了一个接口,那么在Symbian中看到了多重继承却没有看到M类型,合理吗?

最后,来看看CBase这个经常在你面前晃悠的东西吧。

CBase是所有要在heap上构造的基类,所有CBase的子类名都以C开头。具有两个重要特性:a)析构函数是虚的;b)通过CBase::operator new()的特殊版本给所有C类型变量初始化为0,不要担心构造函数没给这些变量初始化啦,这样即使出现异常,已经部分初始化的对象也能得以体面地析构。

问题:

多重继承的时候有没有顺序要求呢?

class CProtocolProvider : public CBase, public MProtocol

CBase必须在MProtocol前面吗?为什么?

阅读(2124) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-01-08 14:34:21

为什么我饿M类接口无法实现

chinaunix网友2008-08-06 14:24:54

是的,CBase,主继承C类必须放在M类的前面。以避免继承时出现的问题