在C ++中没有虚拟的多态性 [英] Polymorphism without virtual in C++

查看:69
本文介绍了在C ++中没有虚拟的多态性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部

我相信这是一个老问题。但我发现一个有趣的设计

关于这个:C ++类中没有虚函数的多态。

我的解决方案适用于某些特殊情况,相信我,非常特别。

1单根类树

2叶子(最低级别)类是密封的,这意味着我们不应该从它们继承类.b br继承类。 />
3 PImpl成语。在根类中只有一个数据mumber,并且

在子类和虚拟功能中没有任何其他数据。


在我的解决方案中,析构函数root类不是虚拟的,但是我们
可以使用基类指针指向派生类对象。


我的问题是:这个设计是否遵循C ++标准?我在

VS2005中进行了测试。没关系。 GCC怎么样?我记得这个非虚拟的

析构函数在C ++标准中是不确定的。


这是一个简单的代码示例:


班级基础

{

公开:

~base()

{

删除[] p;

};


受保护:

int * p;


base():p(new int [10])

{

};


base(int * pp):p(pp)

{

};

};


类base1:公共基础

{

受保护:

base1()

{

};

};


class my:public base1

{

public:

my()

{

p = new int [10];

} ;

};


int _tmain(int argc,_TCHAR * argv [])

{


base1 * o = new my;


删除o;

返回0;

}

解决方案

感觉写道:


全部

我相信这是一个老问题。但我只是发现了一个有趣的设计

:在C ++类中没有虚函数的多态。



我的问题是,为什么?


我的解决方案适用于某些特殊情况,相信我,非常特别。

1单根类树

2叶子(最低级别)类是密封的,这意味着我们不应该b
继承他们的课程。

3 PImpl成语。在根类中只有一个数据mumber,并且

在子类和虚拟功能中没有任何其他数据。


在我的解决方案中,析构函数root类不是虚拟的,但是我们
可以使用基类指针指向派生类对象。


我的问题是:这个设计是否遵循C ++标准?我在

VS2005中进行了测试。没关系。 GCC怎么样?我记得这个非虚拟的
析构函数在C ++标准中是未定义的。



正确。在没有虚析构函数的情况下,通过基类指针删除派生类对象

是未定义的行为。


> ;

这是一个简单的代码示例:


班级基础

{

public:

~base()

{

delete [] p;

};



删除所有函数体后面的分号。它们是多余的

(虽然不是错误)。


>

受保护:

int * p;


base():p(new int [10])

{

} ;


base(int * pp):p(pp)



这里需要评论''base''获得传递给

的指针的所有权。此外,可能想要声明这个c-tor明确。


{

};

};


类base1:公共基础

{

受保护:

base1()< br $>
{

};

};


class my:public base1

{

public:

my()

{

p = new int [10];



这里的内存泄漏。


};

};


int _tmain(int argc,_TCHAR * argv [])



没有标准函数''_ tmain''。没有标准类型

''_ TCHAR''。您必须忘记包含正确的标题[s]。

不要在不关闭扩展名的情况下使用VC ++检查代码。


{


base1 * o = new my;


删除o;



Ka-boom!未定义的行为。


返回0;

}



V

-

请在通过电子邮件回复时删除资金''A'

我不回复热门回复,请不要问


Victor Bazarov< v。******** @ comAcast.netwrote in news:g7cmb5


6so

Hi,All
I am sure it''s an old question. But I just find a interesting design
about this: Polymorphism without virtual function in a C++ class.
My solution is for some special case, trust me, very special.
1 single root class tree
2 the leaf(lowest level) classes are sealed which means we should not
inherite class from them.
3 PImpl idiom. There is only one data mumber in root class and there
is no any other data mumber in child class and virtual funtions.

In my solution, the destructor of root class is not virtual, but we
can use base class pointer to point derived class object.

My question is: is this design follow the C++ standard? I tested it in
VS2005. it''s ok. How about GCC?I remember that this non-virtual
destructor behavor is undefine in C++ standard.

Here is a simple code example:

class base
{
public:
~base()
{
delete[] p;
};

protected:
int *p;

base():p(new int[10])
{
};

base(int *pp) : p(pp)
{
};
};

class base1 : public base
{
protected:
base1()
{
};
};

class my : public base1
{
public:
my ()
{
p = new int[10];
};
};

int _tmain(int argc, _TCHAR* argv[])
{

base1 *o = new my;

delete o;
return 0;
}

解决方案

feel wrote:

Hi,All
I am sure it''s an old question. But I just find a interesting design
about this: Polymorphism without virtual function in a C++ class.

My question is, "why?"

My solution is for some special case, trust me, very special.
1 single root class tree
2 the leaf(lowest level) classes are sealed which means we should not
inherite class from them.
3 PImpl idiom. There is only one data mumber in root class and there
is no any other data mumber in child class and virtual funtions.

In my solution, the destructor of root class is not virtual, but we
can use base class pointer to point derived class object.

My question is: is this design follow the C++ standard? I tested it in
VS2005. it''s ok. How about GCC?I remember that this non-virtual
destructor behavor is undefine in C++ standard.

Correct. It''s undefined behaviour to delete the derived class object
through the base class pointer in the absence of a virtual destructor.

>
Here is a simple code example:

class base
{
public:
~base()
{
delete[] p;
};

Drop the semicolons after all the function bodies. They are superfluous
(although not an error).

>
protected:
int *p;

base():p(new int[10])
{
};

base(int *pp) : p(pp)

Need a comment here that ''base'' takes ownership of the pointer passed to
it. Also, probably want to declare this c-tor "explicit".

{
};
};

class base1 : public base
{
protected:
base1()
{
};
};

class my : public base1
{
public:
my ()
{
p = new int[10];

Memory leak here.

};
};

int _tmain(int argc, _TCHAR* argv[])

There is no standard function ''_tmain''. There is no standard type
''_TCHAR''. You must have forgotten to include the proper header[s].
Don''t check your code with VC++ without turning off extensions.

{

base1 *o = new my;

delete o;

Ka-boom! Undefined behaviour.

return 0;
}

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Victor Bazarov <v.********@comAcast.netwrote in news:g7cmb5


6so


这篇关于在C ++中没有虚拟的多态性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆