原文地址:http://www.cnjm.net/tech/article4202.html
译者: 无牙老虎 http://blog.csdn.net/wenstory/archive/2008/05/25/2480097.aspx
为什么 CBase 有私有 (private) 复制构造函数和私有 (private) operator = 函数 ?
让我们一个个来解决这些问题 .
这是个很有趣的问题 , 清洁栈 (CleanupStack) 有 3 个版本的 PushL(), 他们分别是 :PushL(TAny *aPtr), PushL(CBase *aPtr) 和 PushL(TCleanupItem anItem), 为什么不是只有 PushL(TAny *aPtr) 和 PushL(TCleanupItem anItem)? 先让我们看看清洁栈 (CleanupStack) 是怎么工作的 . 通常情况下我们的代码是这样的 :
CTest* test = CTest::NewL(); //CTest 为 CBase 派生类
CleanupStack::PushL( test );
test->FunL();
CleanupStack::PopAndDestroy();
这是清洁栈的通用用法 , 由于 FunL() 可能会异常退出 , 所以我们将 ”test” 指针压入清洁栈接下来如果一切正常 , 则弹出指针并销毁对象 . 让我们关注一下清洁栈是如何通过调用 PopAndDestroy() 销毁对象的 , 根据 SDK helper 的说明 ,” 如果清洁栈中的项为 CBase 类指针 , 则将该指针从清洁栈中移走并调用 delete 销毁 , 如果为 TAny 类指针 , 则将该指针从清洁栈中移走并调用 User::Free() 释放对象所占有的内存 .”
CBase* test = CTest::NewL(); // CTest 为 CBase 派生类
CleanupStack::PushL( test );
test->FunL();
CleanupStack::PopAndDestroy();
非常幸运 , 由于 Cbase 所有的 new operator 函数都是 inline 的 , 我们能通过 e32base.inl 看到每个函数的实现 , 例如 , "TAny* operator new(TUint aSize, TLeave)" 的实现如下
{ return User::AllocZL(aSize); }
CTest* CTest::NewLC()
{
CTest* self = new ( ELeave ) CTest;
CleanupStack::PushL( self );
self->ConstructL()
return self;
}
void CTest::ConstructL()
{
iPointer = CMustLeave::NewL(); // 假设这里异常退出
}
CTest::~CTest()
if( iPointer )
{
delete iPointer;
iPointer = NULL;
}
}
CBase 的 new operator 有很多个重载版本 , 但是却没有 new[] operator 版本 , 所以如果你使用 new[] 创建 CBase 对象 , 则你得到的对象并没有初始化为二进制 0 值 , 如果你想创建 CBase 派生类数组 , 你可以使用 RPointerArray 等类去实现 .
CBase base = *pointer; // 调用复制构造函数
CBase base;
base = *pointer; // 调用 operator =
调用 operator = 函数时编译器会显示同样的提示 , 如果你真的想进行深度复制 , 你可以实现自己的公有的复制构造函数和 operator = 函数 .CBase 这么做的原因是 : 很多时候你在自己的 CBase 派生类内部申请了堆内存 , 对这写类使用复制构造函数和 operator = 函数是没有意义的 ( 或者我可以说是很危险的 ). 所以 CBase 默认就关闭了这些特性 .
事实上,在symbian中提供自己的公有复制构造函数和operator =函数并不是个好主意.因为这2个函数都不是异常退出函数(这里是指这2个函数名没有以L结尾----译者),但是这2个函数内部的代码可能会异常退出(调用了new (ELeave) 或 NewL()),这是个矛盾点,较好的习惯就是提供一个异常退出函数名实现复制,例如CloneL().
Inside CBase class - Six Essential Questions
Tutorial posted August 1st, 2007 by rensijie in
Basics
Platforms:
Symbian OS
Keywords:
CBase
Everybody knows C-class in Symbian, the so called C-class is the one derived from class CBase. CBase is widely used in Symbian APIs, because it represents the class which should be created on heap. Every Symbian programmer knows how to call NewL() or NewLC() ( may be new (ELeave) ) of the CBase derived class to create the object, but not many people would really look into the CBase class itself to see why it has some interesting features.
If you can answer the following questions, you can skip this article, because you are a Symbian programmer with strong curiosity. If you are not sure about some answers, I recommend you to read this ariticle, because CBase class is essential in Symbian OS and it's interesting to know some features of this class. The questions are:
Why does cleanup stack has 3 versions of PushL() including PushL( CBase *aPtr )?
Why does CBase have a public virtual destructor?
How is CBase derived object initialized to binary zeroes?
Why is CBase derived object initialized to binary zeroes?
Why use new[] to initialize CBase derived object is not recommended?
Why does CBase has a private copy constructor and a private operator = function?
Let's get into these questions one by one.
Why does cleanup stack has 3 versions of PushL() including PushL(CBase *aPtr)?
It's an interesting question, there're 3 versions of PushL() in CleanupStack, they're PushL(TAny *aPtr), PushL(CBase *aPtr) and PushL(TCleanupItem anItem), why not just PushL(TAny *aPtr) and PushL(TCleanupItem anItem)? Let's see how cleanup stack works. Usually we use the code like this:
CTest* test = CTest::NewL(); // CTest is a CBase derived class
CleanupStack::PushL( test );
test->FunL();
CleanupStack::PopAndDestroy();
It's the regular use of cleanup stack, push the pointer "test" into the cleanup stack because FunL() may leave, after that, if everything is fine, pop the pointer and destory the object. Let's consider how does cleanup stack destory the object when calling PopAndDestroy(), according to the SDK helper,
"If the item on the stack is a CBase* pointer, the pointer is removed from the stack and the object is destroyed with delete. If the item on the stack is a TAny* pointer, the pointer is removed from the stack and the memory occupied by the object is freed with User::Free()."
Why does cleanup stack has to judge if the pointer's type is CBase* or TAny*? Becasue a class may provide a private destructor! If a class has a private destructor, calling delete on this pointer will be invalid. In this case, system only calls User::Free() to free the memory of the object itself but can't invoke its destructor.
What happens to CBase derived class? If you take a look at e32base.h(the declaration of CBase is inside, actually part of the declaration), you will find CBase has a public virtual destructor. This ensures the cleanup stack can call delete on the CBase and its derived classes' pointers. It's useful to keep this in mind that if you push a non-CBase class pointer into the cleanup stack, the stack won't call your class's destructor. So, in most of the cases, you would like to either push CBase derived class into cleanup stack or never allocate heap memory in other types of classes.
But if you really want to allocate heap memory in other types of classes, the third version of PushL() can help you out. What you need to do is define a function which will do the cleanup and wrap the object by TCleanupItem.
Why does CBase have a public virtual destructor?
We can divide this question into 2 parts, why virtual, why public? The answer above tells you why public. The reason to make it virtual is simple. Sometimes you want to write the code like this:
CBase* test = CTest::NewL(); // CTest is a CBase derived class
CleanupStack::PushL( test );
test->FunL();
CleanupStack::PopAndDestroy();
With the virtual keyword, cleanup stack can make sure it will destroy the object properly by the base class's pointer.
How is CBase derived object initialized to binary zeroes?
Luckily, since all the new operator functions of CBase is inline, we can see the implementation of every function in e32base.inl. For example for "TAny* operator new(TUint aSize, TLeave)" the implementation is :
inline TAny* CBase::operator new(TUint aSize, TLeave)
{ return User::AllocZL(aSize); }
Here it uses User::AllocZL(), it allocates a cell of specified size from the current thread's default heap, clears it to binary zeroes, and leaves if there is insufficient memory in the heap. That's how CBase derived object is initialized to binary zeroes?
Why is CBase derived object initialized to binary zeroes?
Let's consider the code below :
CTest* CTest::NewLC()
{
CTest* self = new ( ELeave ) CTest;
CleanupStack::PushL( self );
self->ConstructL()
return self;
}
void CTest::ConstructL()
{
iPointer = CMustLeave::NewL(); // assume this leaves
}
CTest::~CTest()
{
if( iPointer )
{
delete iPointer;
iPointer = NULL;
}
}
If CBase doesn't initialize the object to binary zero, and you don't initialize the iPointer to NULL manually, the initial value of iPointer is uncertain. Once CMustLeave::NewL() leaves, the value of iPointer is still uncertain(in most of the cases it's not zero). Since in NewLC, CTest was pushed into the cleanup stack, so system will pop the pointer and call CTest's destructor. This will cause the problem, because the if condition will be true and you will call delete on a pointer which doesn't pointer to a legal memory. Mostly program will crash. You will not meet this problem if iPointer was initialized to zero(NULL).
Why use new[] to initialize CBase derived object is not recommended?
There're a number of overloaded new operator functions in CBase class, but there's no new[] operator function. So if you use new[] to create CBase objects, you will not get the memory with binary zero. If you want to create a array of CBase derived class you can use the class like RPointerArray to deal with it.
Why does CBase has a private copy constructor and a private operator = function?
This is a general method to prevent the developer from the shallow copy accidently. If you write the code like this :
CBase* pointer = new ( ELeave ) CBase;
CBase base = *pointer; // call copy constructor
The compiler will complain "illegal access from CBase to protected/private member CBase::CBase(const CBase&)", because the second line will try to call the copy constructor of CBase. If you write the code like :
CBase* pointer = new ( ELeave ) CBase;
CBase base;
base = *pointer; // call operator =
The compiler will also complain because it will call the operator = function. If you really want to do the deep copy you can write your own public copy constructor and operator = function. The reason that CBase do this is in most cases you will allocate some heap memory inside a CBase derived class, and it doesn't make sense(or I can say it's dangerous)to use the default copy constructor or default operator = function of this kind of class. So CBase turns this feature off by default.
Actually, in Symbian, to provide your own public version of copy contructor or operator = function is not a good idea neither. Because these 2 function are not leaving functions, but the code inside these 2 functions may leave sometimes( will call new (ELeave) or NewL() ). That's a paradox. The good manner is to provide a leaving function named, let's say, CloneL() to do the copy task.