了解临时对象/获取地址 [英] Understanding temporary objects / taking address

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

问题描述

我有一个函数可以将对象的地址作为参数。

它与对象的内容进行一些比较然后返回。

没有参考或者指向该对象的指针存储或将在

函数返回后使用。


假设函数名为f且对象类应该是T $ / $
看起来像这样:


bool f(T * thing){

返回东西 - > foob​​ar == 5; //只是一个愚蠢的例子

}


当我现在写一段这样的代码:


bool yesorno = f(& T(77));


我收到警告,因为我拿了一个临时物品的地址。

我不确定如果这是可以的或不在这里。

对象是否活得足够长,以便f可以对它进行操作?


通常一个好主意对于这样的事情?

或者我应该创建一个额外的局部变量来保存T(77)?

(因为我会有很多这样的调用,因为我是不方便的使用

T的不同子类你可以想象使用new和删除

每次调用都会更加简单。)


TIA

Henning

I have a function which gets the adress of an object as argument.
It does some comparsion with the object''s contents and then returns.
no Reference or pointer to the object is stored or will be used after
the function has returned.

Say the function whould be named f and the objects class whould be T
it''ll look like this:

bool f(T* thing) {
return thing->foobar == 5; // Just a stupid example
}

When I now write a piece of code like this:

bool yesorno = f(&T(77));

I get a warning because I take the address of a temporary object.
Im not sure if this is okay or not here.
Does the object live long enough so f can do its operations on it?

Is it generally a good idea to to such things?
Or should I create an extra local variable which holds T(77)?
(As I will have lots of these calls that whould be unhandy as I use
different subclasses of T and you can Imagine using new and delete for
every call whuld be even unhandier.)

TIA
Henning

推荐答案

文章< e7 ********** @ rzcomm2.rz .tu-bs.de>,

Henning Hasemann< h。******** @ tu-bs.de>写道:
In article <e7**********@rzcomm2.rz.tu-bs.de>,
Henning Hasemann <h.********@tu-bs.de> wrote:
我有一个函数,它将对象的地址作为参数。
它与对象的内容进行一些比较,然后返回。
没有参考或指向对象的指针存储或将在函数返回后使用。

说这个函数应该命名为f,对象类应该是T
它看起来像这样:

bool f(T * thing){
返回东西 - > foobar == 5; //只是一个愚蠢的例子
}

当我现在写一段这样的代码:

bool yesorno = f(& T(77)) ;

我得到一个警告,因为我拿了一个临时物品的地址。
我不确定这是否合适。
这个物体的寿命是否足够长可以对它进行操作吗?

这样的事情通常是个好主意吗?
或者我应该创建一个额外的局部变量来保存T(77)?
(因为我会使用不同的T类的子类,所以我会有很多这些调用,你可以想象使用new和删除
每个调用甚至更简单。)
I have a function which gets the adress of an object as argument.
It does some comparsion with the object''s contents and then returns.
no Reference or pointer to the object is stored or will be used after
the function has returned.

Say the function whould be named f and the objects class whould be T
it''ll look like this:

bool f(T* thing) {
return thing->foobar == 5; // Just a stupid example
}

When I now write a piece of code like this:

bool yesorno = f(&T(77));

I get a warning because I take the address of a temporary object.
Im not sure if this is okay or not here.
Does the object live long enough so f can do its operations on it?

Is it generally a good idea to to such things?
Or should I create an extra local variable which holds T(77)?
(As I will have lots of these calls that whould be unhandy as I use
different subclasses of T and you can Imagine using new and delete for
every call whuld be even unhandier.)




尝试将''f'改为:


bool f(const T * thing);



Try changing ''f'' to:

bool f( const T* thing );




" Henning Hasemann" < H ******** @ tu-bs.de>。在消息中写道

新闻:e7 ********** @ rzcomm2.rz.tu-bs.de ...

"Henning Hasemann" <h.********@tu-bs.de> wrote in message
news:e7**********@rzcomm2.rz.tu-bs.de...
我有一个功能获取对象的地址作为参数。
它与对象的内容进行一些比较然后返回。
没有存储或将在
之后使用对象的指针或指针函数已经返回。

假设函数名为f,对象类应该是T
它看起来像这样:

bool f( T * thing){
返回东西 - > foobar == 5; //只是一个愚蠢的例子
}

当我现在写一段这样的代码:

bool yesorno = f(& T(77)) ;

我得到一个警告,因为我拿了一个临时物品的地址。
我不确定这是否合适。
这个物体的寿命是否足够长可以对它进行操作吗?

这样的事情通常是个好主意吗?
或者我应该创建一个额外的局部变量来保存T(77)?
(因为我会使用不同的T类的子类,所以我会有很多这些调用,你可以想象使用new和删除
每个调用甚至更简单。)
I have a function which gets the adress of an object as argument.
It does some comparsion with the object''s contents and then returns.
no Reference or pointer to the object is stored or will be used after
the function has returned.

Say the function whould be named f and the objects class whould be T
it''ll look like this:

bool f(T* thing) {
return thing->foobar == 5; // Just a stupid example
}

When I now write a piece of code like this:

bool yesorno = f(&T(77));

I get a warning because I take the address of a temporary object.
Im not sure if this is okay or not here.
Does the object live long enough so f can do its operations on it?

Is it generally a good idea to to such things?
Or should I create an extra local variable which holds T(77)?
(As I will have lots of these calls that whould be unhandy as I use
different subclasses of T and you can Imagine using new and delete for
every call whuld be even unhandier.)




我更喜欢使用const引用作为参数,我自己。这让你可以很好地传递一个临时对象,没有任何警告,(并且还可以像指针一样正确处理

多态)。我只使用指针,如果我有
到。


-Howard



I''d prefer to use a const reference as the parameter, myself. That lets you
pass a temporary object fine, with no warnings, (and also handles the
polymorphism correctly, just like a pointer). I only use pointers if I have
to.

-Howard


Henning Hasemann发布:

Henning Hasemann posted:

假设该函数名为f,对象类应为T
它将如下所示:

bool f(T * thing){
返回东西 - > foobar == 5; //只是一个愚蠢的例子
}

当我现在写一段这样的代码:

bool yesorno = f(& T(77)) ;

我收到警告,因为我拿了一个临时物品的地址。
我不确定这是否合适。
Say the function whould be named f and the objects class whould be T
it''ll look like this:

bool f(T* thing) {
return thing->foobar == 5; // Just a stupid example
}

When I now write a piece of code like this:

bool yesorno = f(&T(77));

I get a warning because I take the address of a temporary object.
Im not sure if this is okay or not here.



该警告应该是一个错误。


临时无名对象是一个R值。你不能取

a R值的地址。


如果你真的想把一个临时的地址传递给一个函数,试试

这个:


类ArbitraryClass {

public:

int a;

int b;


ArbitraryClass():a(1),b(2){}

};

bool Func(const ArbitraryClass * p)

{

return p-> a == p-> b;

}


模板< class T>

const T * AdrsTmp(const T& temp){return& temp; }

int main()

{

Func(AdrsTmp(ArbitraryClass()));

}

临时生效直到声明结束;所以上述代码中的临时代码在main的分号结尾处被杀死。


我想知道如果人们会想到的话我重写了AdrsTmp如下:


模板< class T>

T * AdrsTmp(const T& temp){return const_cast< T *>(&温度); }

我们知道,如果

问题中的对象实际上是非常量的,那么抛弃const就完全没问题。暂时的主要是非常规的 -​​

这是肯定的。然而......是AdrsTmp。函数可以自由创建

另一个临时的,当它接受它的参数时,而不是使用由main提供的

临时... ...

- -


Frederick Gotham


That warning should be an error.

A temporary, nameless object is an R-value. You can''t take the address of
a R-value.

If you really want to pass the address of a temporary to a function, try
this:

class ArbitraryClass {
public:
int a;
int b;

ArbitraryClass() : a(1), b(2) {}
};

bool Func( const ArbitraryClass *p )
{
return p->a == p->b;
}

template<class T>
const T *AdrsTmp( const T &temp ) { return &temp; }
int main()
{
Func( AdrsTmp( ArbitraryClass() ) );
}
A temporary lives on until the end of the statement; so the temporary in
the above code gets killed at the end of the semi-colon in "main".

I wonder what people would think if I rewrote "AdrsTmp" like as follows:

template<class T>
T *AdrsTmp( const T &temp ) { return const_cast<T*>(&temp); }
We know that it''s pefectly okay to cast away constness if the object in
question is in fact non-const. The temporary in "main" is non-const --
that''s for sure. However... is the "AdrsTmp" function free to create
another temporary when it takes its argument, rather than work with the
temporary provided by main... ?
--

Frederick Gotham


这篇关于了解临时对象/获取地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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