使用动态创建的对象创建临时对象 [英] creating a temporary object using dynamically created object

查看:63
本文介绍了使用动态创建的对象创建临时对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


如果我使用动态创建的对象'

指针创建一个临时对象,那么当临时对象被销毁时,将会

动态创建的对象也会被销毁吗?我的猜测是它没有被破坏,但是我不确定。


我有以下程序:


#include< iostream>


使用命名空间std;


A类{
$ b $公开:

int x;

A():x(10){}

};


B级{

公开:

A * p;

B():p(0){}

B(A * a):p(a){}

B& operator =(const B& s){

if(& s!= this){

delete p;

p = s.p; //复制s'的指针,所以他们应该指向相同的

对象

}

}

};

int main(){

B b;

b = new A;

cout< < b.p-> x<<结束;

返回0;

}


在主要中函数,我首先使用new创建一个A对象,然后将一个A *指针转换为B对象,这是临时的。然后,

我将这个临时对象分配给b,这样b'的指针p就可以了。应该

也指向动态创建的A对象。完成此复制后,将销毁临时B对象。我试图输出

的值为bp-> x,并且它仍然具有值10.但是,它可能

仍然意味着该值是垃圾,即动态创建的对象A

对象可能已被销毁。


谢谢,

Jess

Hello,

If I create a temporary object using a dynamically created object''s
pointer, then when the temporary object is destroyed, will the
dynamically created object be destroyed too? My guess is that it''s
not destroyed, but I''m not sure.

I have the following program:

#include<iostream>

using namespace std;

class A{
public:
int x;
A():x(10){}
};

class B{
public:
A* p;
B():p(0){}
B(A* a):p(a){}
B& operator=(const B& s){
if(&s != this){
delete p;
p = s.p; //copy s''s pointer, so they should point to the same
object
}
}
};
int main(){
B b;
b = new A;
cout << b.p->x << endl;
return 0;
}

In the "main" function, I first created an A object using "new", then
the A* pointer is converted to a B object, which is temporary. Then,
I assign this temporary object to "b", so that b''s pointer "p" should
also point to the dynamically created A object. After this copying is
done, the temporary B object should be destroyed. I tried to output
the value of "b.p->x", and it still has the value 10. However, it may
still mean the value is garbage, i.e. the dynamically created object A
object may have been destroyed.

Thanks,
Jess

推荐答案

Jess写道:
Jess wrote:

如果我使用动态创建的对象创建临时对象'是
指针,那么当临时对象被销毁时,

动态创建的对象也会被销毁吗?我的猜测是它没有被破坏,但是我不确定。


我有以下程序:


#include< iostream>


使用命名空间std;


A类{
$ b $公开:

int x;

A():x(10){}

};


B级{

公开:

A * p;

B():p(0){}

B(A * a):p(a){}

B& operator =(const B& s){

if(& s!= this){

delete p;

p = s.p; //复制s'的指针,所以他们应该指向相同的

对象

}

}

};


int main(){

B b;

b = new A;

cout<< b.p-> x<<结束;

返回0;

}


在主要中函数,我首先使用new创建一个A对象,然后将一个A *指针转换为B对象,这是临时的。
If I create a temporary object using a dynamically created object''s
pointer, then when the temporary object is destroyed, will the
dynamically created object be destroyed too? My guess is that it''s
not destroyed, but I''m not sure.

I have the following program:

#include<iostream>

using namespace std;

class A{
public:
int x;
A():x(10){}
};

class B{
public:
A* p;
B():p(0){}
B(A* a):p(a){}
B& operator=(const B& s){
if(&s != this){
delete p;
p = s.p; //copy s''s pointer, so they should point to the same
object
}
}
};
int main(){
B b;
b = new A;
cout << b.p->x << endl;
return 0;
}

In the "main" function, I first created an A object using "new", then
the A* pointer is converted to a B object, which is temporary.



正确。

Right.


然后,

我将此临时对象分配给b,使得b'的指针p与b相对应。应该

也指向动态创建的A对象。完成此复制后,将销毁临时B对象。
Then,
I assign this temporary object to "b", so that b''s pointer "p" should
also point to the dynamically created A object. After this copying is
done, the temporary B object should be destroyed.



但是''b''保留了指针,你在

赋值运算符中将它复制到''b''。 br />

But ''b'' retained that pointer, you copied it to ''b'' yourself in the
assignment operator.


我试图输出

的值为bp-> x,并且它仍然具有值10.但是,它可能

仍然意味着值是垃圾,即动态创建的对象A

对象可能已被破坏。
I tried to output
the value of "b.p->x", and it still has the value 10. However, it may
still mean the value is garbage, i.e. the dynamically created object A
object may have been destroyed.



不,它还活着并且很好。


你的例子很好地说明了终身差异和什么

称为所有权。基本上,当你说''新A'时,没有人/拥有/对象''''你

。临时的''B''并不拥有它,只有

有一个指向该对象的指针。 ''b''也不拥有它,只需要从临时的指针复制




既然没有人拥有'' 'A'',没人会摧毁它。该对象存在于

,直到程序完成执行。这是一种内存泄漏。


如果您认为B对象应该拥有A对象的所有权

传递给它[通过指针],然后你应该(a)注意删除

''B''析构函数中的对象(你甚至没有定义) ),并且

(b)负责转让操作员的所有权转移

,这很可能会引用非常数''B'',因为你转移''p''值的所有权时,你需要将''s.p''设置为0。


V
-

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

我没有回复最热门的回复,请不要不要问

No, it''s still alive and well.

Your example is a good illustration of lifetime differences and what is
known as "ownership". Essentially, nobody /owns/ the object ''A'' you
create when you say ''new A''. The temporary ''B'' doesn''t own it, it only
has a pointer to that object. ''b'' doesn''t own it either, it only takes
the copy of the pointer from the temporary.

Since nobody owns the ''A'', nobody destroys it. The object lives on
until the program finishes executing. That''s a kind of "memory leak".

If you decide that a ''B'' object should take ownership of the ''A'' object
passed to it [by the pointer], then you should (a) take care of deleting
the object in the ''B'' destructor (which you didn''t even define), and
(b) take care of the transfer of ownership in the assignment operator
which should most likely take a reference to non-const ''B'' because you
will need to set ''s.p'' to 0 upon transferring the ownership of ''p'' value.

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


5月17日上午8:40,Jess< w ... @ hotmail.comwrote:
On May 17, 8:40 am, Jess <w...@hotmail.comwrote:

您好,


如果我使用dynamica创建临时对象lly创建了对象'的
指针,然后当临时对象被销毁时,

动态创建的对象也会被销毁吗?我的猜测是它没有被破坏,但是我不确定。


我有以下程序:


#include< iostream>


使用命名空间std;


A类{

public:

int x;

A():x(10){}


};


B级{

公开:

A * p;

B():p(0){ }

B(A * a):p(a){}

B& operator =(const B& s){

if(& s!= this){

delete p;

p = s.p; //复制s的指针,所以他们应该指向相同的

对象

}

}

};


int main(){

B b;

b = new A;

cout<< b.p-> x<<结束;

返回0;


}


在主要中函数,我首先使用new创建一个A对象,然后将一个A *指针转换为B对象,这是临时的。然后,

我将这个临时对象分配给b,这样b'的指针p就可以了。应该

也指向动态创建的A对象。完成此复制后,将销毁临时B对象。
Hello,

If I create a temporary object using a dynamically created object''s
pointer, then when the temporary object is destroyed, will the
dynamically created object be destroyed too? My guess is that it''s
not destroyed, but I''m not sure.

I have the following program:

#include<iostream>

using namespace std;

class A{
public:
int x;
A():x(10){}

};

class B{
public:
A* p;
B():p(0){}
B(A* a):p(a){}
B& operator=(const B& s){
if(&s != this){
delete p;
p = s.p; //copy s''s pointer, so they should point to the same
object
}
}

};

int main(){
B b;
b = new A;
cout << b.p->x << endl;
return 0;

}

In the "main" function, I first created an A object using "new", then
the A* pointer is converted to a B object, which is temporary. Then,
I assign this temporary object to "b", so that b''s pointer "p" should
also point to the dynamically created A object. After this copying is
done, the temporary B object should be destroyed.



临时销毁但p仍然指向未删除的内存。所以

没有坏处。

在这种情况下如果你写这样的析构函数会有问题

~B(){delete p;}

我试图输出

Temporary is destroyed but p is still pointing to undeleted memory. So
no harm.
In this case it will be problematic if you write destructor like this
~B(){delete p;}
I tried to output


bp-> x的值,它仍然具有值10.
the value of "b.p->x", and it still has the value 10.



因为它应该是

As it should


然而,它可能

仍然意味着价值是垃圾,即动态创建的对象A / $
对象可能已被销毁。
However, it may
still mean the value is garbage, i.e. the dynamically created object A
object may have been destroyed.



NO

NO


>

谢谢,

Jess
>
Thanks,
Jess



5月17日上午9:33,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
On May 17, 9:33 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

Jess写道:
Jess wrote:

如果我使用动态创建的对象'

指针创建一个临时对象,然后当临时对象被销毁时,

动态创建的对象是否也会被销毁?我的猜测是,它没有被摧毁,但我不确定。
If I create a temporary object using a dynamically created object''s
pointer, then when the temporary object is destroyed, will the
dynamically created object be destroyed too? My guess is that it''s
not destroyed, but I''m not sure.


我有以下程序:
I have the following program:


#include< iostream> ;
#include<iostream>


using namespace std;
using namespace std;


A级{

public:

int x;

A():x(10){}

};
class A{
public:
int x;
A():x(10){}
};


class B {

public:

A * p;

B():p(0){}

B(A * a):p(a){}

B& operator =(const B& s){

if(& s!= this){

delete p;

p = s.p; //复制s'的指针,所以他们应该指向相同的

对象

}

}

};
class B{
public:
A* p;
B():p(0){}
B(A* a):p(a){}
B& operator=(const B& s){
if(&s != this){
delete p;
p = s.p; //copy s''s pointer, so they should point to the same
object
}
}
};


int main(){

B b;

b = new A;

cout<< b.p-> x<< endl;

返回0;

}
int main(){
B b;
b = new A;
cout << b.p->x << endl;
return 0;
}


在main中函数,我首先使用new创建一个A对象,然后将一个A *指针转换为B对象,这是临时的。
In the "main" function, I first created an A object using "new", then
the A* pointer is converted to a B object, which is temporary.



正确。


Right.


然后,

我将此临时对象分配给b,使得b'的指针p与b相对应。应该

也指向动态创建的A对象。完成此复制后,将销毁临时B对象。
Then,
I assign this temporary object to "b", so that b''s pointer "p" should
also point to the dynamically created A object. After this copying is
done, the temporary B object should be destroyed.



但是''b''保留了指针,你在

赋值运算符中将它复制到''b''。 br />


But ''b'' retained that pointer, you copied it to ''b'' yourself in the
assignment operator.


我试图输出

的值为bp-> x,并且它仍然具有值10.但是,它可能

仍然意味着值是垃圾,即动态创建的对象A

对象可能已被破坏。
I tried to output
the value of "b.p->x", and it still has the value 10. However, it may
still mean the value is garbage, i.e. the dynamically created object A
object may have been destroyed.



不,它还活着并且很好。


你的例子很好地说明了终身差异和什么

称为所有权。基本上,当你说''新A'时,没有人/拥有/对象''''你

。临时的''B''并不拥有它,只有

有一个指向该对象的指针。 ''b''也不拥有它,只需要从临时的指针复制




既然没有人拥有'' 'A'',没人会摧毁它。该对象存在于

,直到程序完成执行。这是一种内存泄漏。


如果您认为B对象应该拥有A对象的所有权

传递给它[通过指针],然后你应该(a)注意删除

''B''析构函数中的对象(你甚至没有定义) ),和


No, it''s still alive and well.

Your example is a good illustration of lifetime differences and what is
known as "ownership". Essentially, nobody /owns/ the object ''A'' you
create when you say ''new A''. The temporary ''B'' doesn''t own it, it only
has a pointer to that object. ''b'' doesn''t own it either, it only takes
the copy of the pointer from the temporary.

Since nobody owns the ''A'', nobody destroys it. The object lives on
until the program finishes executing. That''s a kind of "memory leak".

If you decide that a ''B'' object should take ownership of the ''A'' object
passed to it [by the pointer], then you should (a) take care of deleting
the object in the ''B'' destructor (which you didn''t even define), and



问题:当临时创建时它将获得

被破坏的内存p将被删除。

Problem: As temporary is getting created and when it will get
destroyed memory pointed by p will also be deleted.


(b)负责转让运营商的所有权转移

,这很可能会引用非const'' B''因为你需要在转移''p''值的所有权时将's.p'设置为0。
(b) take care of the transfer of ownership in the assignment operator
which should most likely take a reference to non-const ''B'' because you
will need to set ''s.p'' to 0 upon transferring the ownership of ''p'' value.



问题:无法编译。临时创建因此无法将
绑定到非const引用。

Problem:Will not compile. Temporary is getting created so it can not
be bound to non-const reference.


>

V

-

请删除资金''当用电子邮件回复时,我会回答这个问题。我不回复最热门的回复,请不要问 - 隐藏引用的文字 -


- 显示引用的文字 -
>
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask- Hide quoted text -

- Show quoted text -



这篇关于使用动态创建的对象创建临时对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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