struct _GtkWidget
{
/* The object structure needs to be the first
* element in the widget structure in order for
* the object mechanism to work correctly. This
* allows a GtkWidget pointer to be cast to a
* GtkObject pointer.
*/
GtkObject object;
...
};
struct _GtkWidgetClass
{
/* The object class structure needs to be the first
* element in the widget class structure in order for
* the class mechanism to work correctly. This allows a
* GtkWidgetClass pointer to be cast to a GtkObjectClass
* pointer.
*/
GtkObjectClass parent_class;
...
};
以上是从gtk上截取了一段代码,可以看出确实使用了oo的思想,但却没有
Coo的便利,从注释来看可能还要用到强制类型转换.从书写到安全两方面
都不如Coo:
typedef struct _GtkWidget
{
EXTENDS(GtkObject)
...
} _GtkWidget;
typedef struct _GtkWidgetClass
{
EXTENDS(GtkObjectClass)
...
} _GtkWidgetClass;作者: snow888 时间: 2010-06-08 15:58
这帖子没火???作者: pan_0326 时间: 2010-06-09 06:09 本帖最后由 pan_0326 于 2010-06-09 07:15 编辑
虚继承
class CBase
{
public:
int a;
};
class CT1 : public virtual CBase
{
public:
int a1;
};
class CT2 : public virtual CBase
{
public:
int a2;
};
class CThis : public CT1, public CT2
{
public:
int i;
};
上面是C++中虚继承的例子,sizeof(CThis)是24,不是16,因为
CT1和CT2分别嵌入了CBase*来间接访问基类CBase,看看Coo实
现就明白了:
typedef struct CBase
{
int a;
} CBase;
typedef struct _CT1
{
CBase* pCBase; //虚继承
int a1;
} _CT1;
typedef struct _CT2
{
CBase* pCBase; //虚继承
int a2;
} _CT2;
typedef struct CThis
{
EXTENDS(_CT1)
EXTENDS(_CT2)
EXTENDS(CBase) //共享继承
int i;
} CThis;
CThis嵌入了一份CBase共享于_CT1和_CT2,构造函数时装配pCBase.
_CT1和_CT2是不能直接单独使用的,包装后才行:
typedef struct CT1
{
EXTENDS(_CT1)
EXTENDS(CBase)
} CT1;
Coo还可以更灵活,允许其中一条路径是正常继承:
typedef struct CBase
{
int a;
} CBase;
typedef struct CT1
{
EXTENDS(CBase) //正常继承
int a1;
} CT1;
typedef struct _CT2
{
CBase* pCBase; //虚继承
int a2;
} _CT2;
typedef struct CThis
{
EXTENDS(CT1)
EXTENDS(_CT2)
int i;
} CThis;
由CT1提供CBase,共享于_CT2.现在sizeof(CThis)是20,少了一指针.作者: bill15 时间: 2010-06-09 08:31
不必搞那些稀奇古怪的,C++挺好的,钻研它吧
等你成大师,可以推广你的咚咚的时候再拿出来说事作者: pan_0326 时间: 2010-06-09 10:49