auto_ptr正确用法 [英] auto_ptr correct usage
问题描述
我无法相信我无法理解这一点。
我有一个结构类型的auto_ptr类。我有它作为会员
我班上的
变量。
struct Y
{
SOMETYPE&安培; m_st;
Y(SomeType& st):m_st(st){}
};
class X
{
auto_ptr< Ym_Y;
};
X的ctor对我来说太早了初始化m_Y,因为Y包含
a参考成员,当X'的ctor运行时,该成员将无法使用。
中的一个类我的成员函数,我试过:
m_Y =新Y(sometypeObj);
并且这对我很惊讶我调试了一下,我不知道为什么当Y'的ctor
运行时变量不会被初始化。
可以理解这是有效的:
auto_ptr< YtempY(new Y(sometypeObj));
m_Y = tempY;
但我不想这样做!
Dilip写道:
我不能相信我无法理解这个简单的事情。
我有一个结构类型的auto_ptr类。我有它作为会员
我班上的
变量。
struct Y
{
SOMETYPE&安培; m_st;
Y(SomeType& st):m_st(st){}
};
class X
{
auto_ptr< Ym_Y;
};
X的ctor对我来说太早了初始化m_Y,因为Y包含
a参考成员,当X'的ctor运行时,该成员将无法使用。
中的一个类我的成员函数,我试过:
m_Y =新Y(sometypeObj);
并且这对我很惊讶
什么是不起作用意思?不编译?崩溃(UB)?
我调试了一下,我不知道为什么变量不会被初始化Y'的ctor
运行。
有哪些变数?如何定义'sometypeObj'?
>
可以理解这是有效的:
auto_ptr< YtempY(new Y(sometypeObj));
m_Y = tempY;
但我不想这样做!
RTFM,为什么不要?
''std :: auto_ptr''有一个从''auto_ptr_ref定义的赋值运算符'',
并给它新的价值你应该使用''重置''会员。
V
-
请在通过电子邮件回复时删除资金''A'
我没有回复最热门的回复,请不要问
Victor Bazarov写道:
Dilip写道:
m_Y =新Y(sometypeObj);
这并不令我惊讶。
什么是不起作用意思?不编译?崩溃(UB)?
Y的成员变量未正确初始化。他们里面有垃圾
。忘记引用,只是一个带有int
数据类型的简单结构。
struct Y
{
int xx;
Y(int i):xx(i){}
};
m_Y = new Y(1);
不会产生m_Y对象,xx初始化为1(其中m_Y是
类型auto_ptr< Y> )。正如我所说,我使用
调试器(VC 2005)进入auto_ptr代码,我可以看到赋值运算符被执行了
但是一旦执行结束我看不到完全构造的
有效的m_Y对象。
可以理解这是有效的:
auto_ptr< YtempY(new Y(sometypeObj));
m_Y = tempY;
但我不是想要这样做!
RTFM,为什么不要?
''std :: auto_ptr''有一个从''auto_ptr_ref定义的赋值运算符'',
并且为了给它新值,你应该使用''reset''成员。
我收不到。重置代码如下所示:
void reset(_Ty * _Ptr = 0)
{//销毁指定对象并存储新指针
if(_Ptr!= _Myptr)
删除(_Ty *)_ Myptr;
_Myptr = _Ptr;
}
所有我能看到的是包含的指针被删除并重新分配
与传入的那个。我错过了什么?
嗯。为什么你不应该使用类似的东西:
m_Y = auto_ptr< Y(new Y(1));
或者,正如你所写: br />
auto_ptr< YtempY(new Y(1));
m_Y = tempY;
?
Alessandro
10月26日晚上11点34分,Dilip < rdil ... @ lycos.comwrote:
Victor Bazarov写道:
Dilip写道:
m_Y = new Y(sometypeObj);
这并不会让我感到惊讶。
什么是不起作用意思?不编译?崩溃(UB)?Y的成员变量未正确初始化。他们里面有垃圾
。忘记引用,只是一个带有int
数据类型的简单结构。
struct Y
{
int xx;
Y(int i):xx(i){}
}; m_Y = new Y(1) ;
不会产生m_Y对象,xx初始化为1(其中m_Y是
类型auto_ptr< Y>)。正如我所说,我使用
调试器(VC 2005)进入auto_ptr代码,我可以看到赋值运算符被执行了
但是一旦执行结束我看不到完全构造的
有效的m_Y对象。
可以理解这是有效的:
auto_ptr< YtempY(new Y(sometypeObj));
m_Y = tempY;
但我不想这样做!
RTFM,为什么dontcha?
''td :: auto_ptr''有一个从''auto_ptr_ref''定义的赋值运算符,
和给它新的价值,你应该使用''重置''成员。我不明白。重置代码如下所示:
void reset(_Ty * _Ptr = 0)
{//销毁指定对象并存储新指针
如果(_Ptr!= _Myptr)
删除(_Ty *)_ Myptr;
_Myptr = _Ptr;
}
所有我能看到的是包含的指针被删除并重新分配
与传入的那个。我错过了什么?
I can''t believe I am unable to get this simple thing right.
I have an auto_ptr class to a structure type. I have it as a member
variable in my class.
struct Y
{
SomeType& m_st;
Y(SomeType& st) : m_st(st) { }
};
class X
{
auto_ptr<Ym_Y;
};
The ctor of X is too early for me to initialize m_Y because Y contains
a reference member that won''t be available by the time X''s ctor runs.
in one of class X''s member functions, I tried doing:
m_Y = new Y(sometypeObj);
and that doesn''t work much to my surprise. I debugged a bit and I
can''t understand why the variables won''t get initialized when Y''s ctor
is run.
Understandably this works:
auto_ptr<YtempY(new Y(sometypeObj));
m_Y = tempY;
but I don''t want to do this!
Dilip wrote:I can''t believe I am unable to get this simple thing right.
I have an auto_ptr class to a structure type. I have it as a member
variable in my class.
struct Y
{
SomeType& m_st;
Y(SomeType& st) : m_st(st) { }
};
class X
{
auto_ptr<Ym_Y;
};
The ctor of X is too early for me to initialize m_Y because Y contains
a reference member that won''t be available by the time X''s ctor runs.
in one of class X''s member functions, I tried doing:
m_Y = new Y(sometypeObj);
and that doesn''t work much to my surprise.What does "that doesn''t work" mean? Doesn''t compile? Crash (UB)?
I debugged a bit and I
can''t understand why the variables won''t get initialized when Y''s ctor
is run.What variables? How is ''sometypeObj'' defined?
>
Understandably this works:
auto_ptr<YtempY(new Y(sometypeObj));
m_Y = tempY;
but I don''t want to do this!RTFM, why dontcha?
''std::auto_ptr'' has an assignment operator defined from ''auto_ptr_ref'',
and to give it new value you should use ''reset'' member.
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 wrote:Dilip wrote:m_Y = new Y(sometypeObj);
and that doesn''t work much to my surprise.
What does "that doesn''t work" mean? Doesn''t compile? Crash (UB)?The member vars of Y are not properly initialized. They have junk
inside of them. Forget the reference, just a simple struct with a int
data type inside of it.
struct Y
{
int xx;
Y(int i) : xx(i) { }
};
m_Y = new Y(1);
does not yield a m_Y object with xx initialized to 1 (where m_Y is of
type auto_ptr<Y>). As I said I did step inside the auto_ptr code using
the debugger (VC 2005) and I can see the assignment operator being
executed but once the execution ends I cannot see a fully constructed
valid m_Y object.
Understandably this works:
auto_ptr<YtempY(new Y(sometypeObj));
m_Y = tempY;
but I don''t want to do this!
RTFM, why dontcha?
''std::auto_ptr'' has an assignment operator defined from ''auto_ptr_ref'',
and to give it new value you should use ''reset'' member.I don''t get it. The reset code looks like this:
void reset(_Ty* _Ptr = 0)
{ // destroy designated object and store new pointer
if (_Ptr != _Myptr)
delete (_Ty *)_Myptr;
_Myptr = _Ptr;
}
All I can see is the contained pointer being deleted and re-assigned
with the one that was passed in. What am I missing?
hmmm. Why shouldn''t you use something like:
m_Y = auto_ptr<Y( new Y(1) );
or, as you wrote:
auto_ptr<YtempY(new Y(1));
m_Y = tempY;
?
Alessandro
On Oct 26, 11:34 pm, "Dilip" <rdil...@lycos.comwrote:Victor Bazarov wrote:Dilip wrote:m_Y = new Y(sometypeObj);
and that doesn''t work much to my surprise.
What does "that doesn''t work" mean? Doesn''t compile? Crash (UB)?The member vars of Y are not properly initialized. They have junkinside of them. Forget the reference, just a simple struct with a int
data type inside of it.
struct Y
{
int xx;
Y(int i) : xx(i) { }
};m_Y = new Y(1);
does not yield a m_Y object with xx initialized to 1 (where m_Y is of
type auto_ptr<Y>). As I said I did step inside the auto_ptr code using
the debugger (VC 2005) and I can see the assignment operator being
executed but once the execution ends I cannot see a fully constructed
valid m_Y object.
Understandably this works:
auto_ptr<YtempY(new Y(sometypeObj));
m_Y = tempY;
but I don''t want to do this!
RTFM, why dontcha?
''std::auto_ptr'' has an assignment operator defined from ''auto_ptr_ref'',
and to give it new value you should use ''reset'' member.I don''t get it. The reset code looks like this:
void reset(_Ty* _Ptr = 0)
{ // destroy designated object and store new pointer
if (_Ptr != _Myptr)
delete (_Ty *)_Myptr;
_Myptr = _Ptr;
}
All I can see is the contained pointer being deleted and re-assigned
with the one that was passed in. What am I missing?
这篇关于auto_ptr正确用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!