这是有效的道德C ++吗? [英] Is this valid and moral C++?

查看:59
本文介绍了这是有效的道德C ++吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个函数


f(Object *& obj)


并且声明了一个全局std :: vector<对象* vec;


是否有效


void g(){

vec.push_back(新对象);

f(vec.back());

}


即f()内部实际上是否已读取/写入对象的访问权限

在堆上的g()中分配?如果没有,那么实现

的诀窍是什么?谢谢,


filimon

Suppose that we have a function

f(Object*& obj)

and have declared a global std::vector<Object*vec;

Is it valid to do

void g() {
vec.push_back(new Object);
f(vec.back());
}

ie does f() internally actually have read/write access to the Object
allocated in g() on the heap? If not, what would be the trick to achieve
this? Thanks,

filimon

推荐答案

Filimon Roukoutakis写道:
Filimon Roukoutakis wrote:

假设我们有一个函数


f(对象*& obj)


并且已经声明了一个全局std: :vector< Object * vec;


它是否有效


void g(){

vec .push_back(new Object);

f(vec.back());

}


即内部f()实际上对堆上的g()中分配的对象

有读/写权限吗?如果没有,那么实现

的诀窍是什么?谢谢,
Suppose that we have a function

f(Object*& obj)

and have declared a global std::vector<Object*vec;

Is it valid to do

void g() {
vec.push_back(new Object);
f(vec.back());
}

ie does f() internally actually have read/write access to the Object
allocated in g() on the heap? If not, what would be the trick to achieve
this? Thanks,



是的,它是有效的。


在删除之前,任何函数都可以使用new分配的对象。 />
调用它。

Yes, it is valid.

Objects allocated by new are available to any function before delete is
called on it.


On Sun,2007年3月18日21:09:22 +0100 in comp.lang.c ++,Filimon Roukoutakis
< fi ***** @ phys.uoa.grwrote,
On Sun, 18 Mar 2007 21:09:22 +0100 in comp.lang.c++, Filimon Roukoutakis
<fi*****@phys.uoa.grwrote,

>假设我们有一个功能

f(对象*& obj)

并声明了一个全局的std :: vector< Object * vec;

它是否有效

void g(){

vec.push_back(new Object);

f(vec.back());
}
>Suppose that we have a function

f(Object*& obj)

and have declared a global std::vector<Object*vec;

Is it valid to do

void g() {
vec.push_back(new Object);
f(vec.back());
}



编号该函数应按值接收指针参数。即

f(对象* obj)


尝试将可修改的引用传递给

的结果是无效的。 back(),虽然const引用可行,但它没有特别的

优势。


其次,你的Object *的全局向量不会删除对象

指向它的析构函数在程序结束时运行。那时候回收内存并没什么大不了的,但是如果对象'的
析构函数做了其他重要事情你就会遇到问题。

No. The function should receive the pointer argument by value. i.e.
f(Object* obj)

It is invalid to try to pass a modifiable reference to the result of
..back() , and while a const reference would work it has no particular
advantage.

Secondly, your global vector of Object* will NOT delete the objects
pointed to when it''s destructor runs at the end of the program. There''s
no big deal about reclaiming the memory at that point, but if Object''s
destructor does anything else important you''ve got a problem.


>即f()内部实际上是否具有对堆上g()中分配的对象的读/写访问权限?
>ie does f() internally actually have read/write access to the Object
allocated in g() on the heap?



是的。事实上,这是访问你拥有的对象的唯一方法

此时离开了你自己。这很好。

Yes. In fact that is the only way of accessing the object that you have
left yourself at this point, which is fine.


David Harmon写道:
David Harmon wrote:

On Sun,2007年3月18日21:09:22 +0100 in comp.lang.c ++,Filimon Roukoutakis

< fi ***** @ phys.uoa.grwrote,
On Sun, 18 Mar 2007 21:09:22 +0100 in comp.lang.c++, Filimon Roukoutakis
<fi*****@phys.uoa.grwrote,

>假设我们有一个函数

f(对象*& obj)

并声明了一个全局的std :: vector< Object * vec;

它是否有效

void g(){
vec.push_back(new Object);
f(vec.back());
}
>Suppose that we have a function

f(Object*& obj)

and have declared a global std::vector<Object*vec;

Is it valid to do

void g() {
vec.push_back(new Object);
f(vec.back());
}



不可以。该函数应该按值接收指针参数。即

f(对象* obj)


尝试将可修改的引用传递给

.back的结果无效(),虽然const引用可行,但它没有特别的优势。


No. The function should receive the pointer argument by value. i.e.
f(Object* obj)

It is invalid to try to pass a modifiable reference to the result of
.back() , and while a const reference would work it has no particular
advantage.



为什么它无效 ?


Why is it "invalid" ?


这篇关于这是有效的道德C ++吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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