参考临时 [英] Reference to temporary
问题描述
大家好,
下面的代码不合法(foo_t初始化程序列表有问题)
因为:
"不是''const'的引用不能绑定到非左值'
我怎样才能最好地实现类似于此代码的效果尝试?
谢谢,
Dave
class bar_t
{
};
class foo_t
{
public:
foo_t ():ref(bar_t()){}
私人:
bar_t& ref;
};
void foo()
{
foo_t a;
}
>
int main()
{
foo();
返回0;
}
Hello all,
The code below is not legal (problem with the foo_t initializer list)
because:
"A reference that is not to ''const'' cannot be bound to a non-lvalue"
How can I best achieve an effect similar to what this code attempts?
Thanks,
Dave
class bar_t
{
};
class foo_t
{
public:
foo_t(): ref(bar_t()) {}
private:
bar_t &ref;
};
void foo()
{
foo_t a;
}
int main()
{
foo();
return 0;
}
推荐答案
" Dave"其中p是*********** @ yahoo.com>在消息新闻中写道:vq ************ @ news.supernews.com ...
"Dave" <be***********@yahoo.com> wrote in message news:vq************@news.supernews.com...
大家好,
下面的代码是不合法(foo_t初始化列表的问题)
因为:
"不是''const'的引用不能绑定到非左值'
我怎样才能最好地达到与此代码尝试类似的效果?
Hello all,
The code below is not legal (problem with the foo_t initializer list)
because:
"A reference that is not to ''const'' cannot be bound to a non-lvalue"
How can I best achieve an effect similar to what this code attempts?
使用const引用?
Use a const reference?
>
Dave在新闻中写道:vq ************ @ news.supernews.com:
Dave wrote in news:vq************@news.supernews.com:
大家好,
因为:
"不是''const'的引用不能绑定到非lvalue"
我怎样才能最好地达到与此代码尝试类似的效果?
谢谢,
Dave
类bar_t <课程foo_t
公开:
ar_t()){}
私人:
bar_t& ref;
};
Hello all,
The code below is not legal (problem with the foo_t initializer list)
because:
"A reference that is not to ''const'' cannot be bound to a non-lvalue"
How can I best achieve an effect similar to what this code attempts?
Thanks,
Dave
class bar_t
{
};
class foo_t
{
public:
foo_t(): ref(bar_t()) {}
private:
bar_t &ref;
};
你会有告诉我们有什么问题:
class foo_t
{
public:
foo_t (){}
私人:
bar_t ref;
};
即你需要解决什么问题。
Rob。
-
http://www.victim-prime.dsl.pipex.com/
Dave写道:
...
下面的代码不合法(foo_t初始化列表的问题)
因为:
" ;不是''const'的引用不能绑定到非左值'
如何才能最好地实现类似于此代码尝试的效果?
class bar_t
{
};
class foo_t
{
公开:
foo_t():ref(bar_t()){ }
私人:
bar_t& ref;
};
void foo()
{
foo_t a;
}
{
foo();
返回0;
}
...
...
The code below is not legal (problem with the foo_t initializer list)
because:
"A reference that is not to ''const'' cannot be bound to a non-lvalue"
How can I best achieve an effect similar to what this code attempts?
class bar_t
{
};
class foo_t
{
public:
foo_t(): ref(bar_t()) {}
private:
bar_t &ref;
};
void foo()
{
foo_t a;
}
int main()
{
foo();
return 0;
}
...
你不能这样做。你需要一个有效的引用,这是某个类的一个
成员,你需要一个有效的左值对象(即
非临时对象)来初始化它。对象的生命周期应该是
,至少与参考的生命周期一样长。
-
Best问候,
Andrey Tarasevich
You can''t do it this way. It you need a valid reference, which is a
member of some class, you need a valid lvalue object (i.e. a
non-temporary) to initialize it with. The object''s lifetime should be at
least as long as the lifetime of the reference.
--
Best regards,
Andrey Tarasevich
这篇关于参考临时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!