私有析构函数和模板 [英] private destructor and templates

查看:78
本文介绍了私有析构函数和模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个私有析构函数的POD类型。有一个完整的

派生POD类型的层次结构,所有这些都意味着使用公共成员函数释放

在基类中销毁。我收到警告C4624。我读了描述,

决定它正是我想要的,并忽略了警告。


现在我正在尝试继承使用一个模板。而不是析构函数可能因为基类析构函数不可访问而无法生成
,我现在

有错误C2248< base of constru destructor无法访问私有

成员。这是正确的还是错误的?我不希望编译器为基类生成一个

析构函数,我不会在堆栈中声明任何变量

我将使用基数销毁功能以解除分配。


/ **

** \ brief携带请求和任何相关参数。

** /

struct PNPEXPORT IConcurrentOperations :: OpRequest摘要:OpMessage

{

...

private:

/ **

** \ brief析构函数,执行清理

**

**调用FreeAgent管理参考计数pAgent。

**

**私密可见性可防止在堆栈上声明。

** /

~OpRequest()

{

FreeAgent();

}


public :

/ **

** \ brief释放此请求使用的资源

** /

void Destroy(void)

{

删除此内容;

}

};


/ **

** \ param Base,应该来自OpNotification或OpRequest

**并提供默认构造函数。

** /

模板< typename Base>

struct IConcurrentOperations :: BufferedMessage:public Base

{...}

I have a POD type with a private destructor. There are a whole hierarchy of
derived POD types, all meant to be freed using a public member function
Destroy in the base class. I get warning C4624. I read the description,
decided that it''s exactly what I want, and ignored the warning.

Now I''m trying to inherit using a template. Instead of "destructor could
not be generated because a base class destructor is inaccessible", I now
have an error C2248 <name of base destructor"cannot access private
member". Is this correct or a bug? I don''t want the compiler to generate a
destructor for the base class, I won''t declare any variables on the stack
and I will use the base Destroy function to deallocate it.

/**
** \brief Carries a request and any associated parameters.
**/
struct PNPEXPORT IConcurrentOperations::OpRequest abstract : OpMessage
{
...
private:
/**
** \brief Destructor, executes cleanup
**
** Calls FreeAgent to manage reference counting pAgent.
**
** Private visibility prevents declaration on the stack.
**/
~OpRequest()
{
FreeAgent();
}

public:
/**
** \brief Frees resources used by this request
**/
void Destroy( void )
{
delete this;
}
};

/**
** \param Base, should be derived from OpNotification or OpRequest
** and provide a default constructor.
**/
template<typename Base>
struct IConcurrentOperations::BufferedMessage : public Base
{...}

推荐答案

" Ben Voigt" < rb*@nospam.nospamwrote in message

news:ue **************** @ TK2MSFTNGP04.phx.gbl ...
"Ben Voigt" <rb*@nospam.nospamwrote in message
news:ue****************@TK2MSFTNGP04.phx.gbl...

>我有一个带有私有析构函数的POD类型。派生POD类型有一个完整的层次结构,所有这些都意味着使用公共成员函数释放
在基类中销毁。我收到警告C4624。我阅读了描述,
决定它正是我想要的,并忽略了警告。


现在我正在尝试继承使用模板。而不是析构函数可能因为基类析构函数不可访问而无法生成
,我现在

有错误C2248< base of constru destructor无法访问私有

成员。这是正确的还是错误的?我不希望编译器为基类生成

a析构函数,我不会在

堆栈中声明任何变量,我将使用基础Destroy解除分配的功能。
>I have a POD type with a private destructor. There are a whole hierarchy
of derived POD types, all meant to be freed using a public member function
Destroy in the base class. I get warning C4624. I read the description,
decided that it''s exactly what I want, and ignored the warning.

Now I''m trying to inherit using a template. Instead of "destructor could
not be generated because a base class destructor is inaccessible", I now
have an error C2248 <name of base destructor"cannot access private
member". Is this correct or a bug? I don''t want the compiler to generate
a destructor for the base class, I won''t declare any variables on the
stack and I will use the base Destroy function to deallocate it.



模板或否,为了从类派生,该类至少需要一个可访问的构造函数和一个可访问的析构函数。这是一个MS

扩展(或者bug,取决于你的观点),编译器允许你在非模板的情况下使用它来获取它。 />

你能简单地让基类析构函数保护而不是私有吗?


-cd

Template or no, in order to derive from a class, that class needs at least
one accessible constructor and an accessible destructor. It''s an MS
extension (or bug, depending on your viewpoint) that the compiler let you
get away with it in your non-template case.

Can you simply make the base-class destructor protected instead of private?

-cd


Ben Voigt写道:
Ben Voigt wrote:

我有一个私有析构函数的POD类型。
I have a POD type with a private destructor.



没有这样的事情。 POD不能没有私有东西,POD不能有用户定义的析构函数(8.5.1 / 1,9 / 4)。

No such thing. PODs cannot not have private anything, PODs cannot have
user-defined destructors (8.5.1/1, 9/4).


派生POD类型有一个完整的

层次结构,所有这些都意味着使用公共

成员函数在基类中销毁。我收到警告C4624。我看了b
$ b读到这个描述,确定它正是我想要的,并且

忽略了警告。

现在我来了试图继承使用模板。而不是析构函数

无法生成,因为基类析构函数是

无法访问,我现在有一个错误C2248< base of constru destructor>

无法访问私人会员。这是正确的还是错误的?我不希望
希望编译器为基类生成一个析构函数,我不会在栈上声明任何变量而我将使用该库

销毁解除分配函数。 [...]
There are a whole
hierarchy of derived POD types, all meant to be freed using a public
member function Destroy in the base class. I get warning C4624. I
read the description, decided that it''s exactly what I want, and
ignored the warning.
Now I''m trying to inherit using a template. Instead of "destructor
could not be generated because a base class destructor is
inaccessible", I now have an error C2248 <name of base destructor>
"cannot access private member". Is this correct or a bug? I don''t
want the compiler to generate a destructor for the base class, I
won''t declare any variables on the stack and I will use the base
Destroy function to deallocate it. [...]



如果派生类没有私人d-tor,程序将为b $ b尝试提供一个你(无论你是否想要),反过来

将尝试调用你私有的基类d-tor。所以,

从一个带有私人D-tor的班级派生时,再次声明你的d-tor

私有。


V

-

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

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

If the derived class does not have a private d-tor, the program will
try to provide one for you (whether you want it or not), which in turn
will try to call the base class d-tor, which you made private. So,
when deriving from a class with a private d-tor, declare your d-tor
private again.

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


Ben Voigt写道:
Ben Voigt wrote:

我有一个POD类型私人析构函数。
I have a POD type with a private destructor.



很抱歉,这不是POD。如果你有一个用户定义的析构函数,

这是违反POD类型的定义。


如果你派生自非POD类,并且你不提供析构函数,

编译器会为你生成一个。仅仅因为你没有明确的那个,这并不意味着你根本就没有。派生的

析构函数必须能够调用基数,如果它是私有的,那么它不是可以访问的。这本质上意味着你不能从具有私有析构函数的类中获得。这是我的理解。


Tom

I''m sorry but it''s not a POD. If you have a user defined destructor,
that''s against the definition of the POD type.

If you derive from a non-POD class, and you don''t supply a destructor,
the compiler will generate one for you. Just because you don''t have an
explicit one, it doesn''t mean you don''t have one at all. The derived
destructor must be able to call the base, and if it''s private, it''s not
accessible. This essencially means that you can''t derive from a class
that have a private destructor. This is my understanding.

Tom


这篇关于私有析构函数和模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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