分配给参考 [英] Assigning to references

查看:62
本文介绍了分配给参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


请考虑以下代码。它代表了一个问题我没有b $ b。


foo_t需要包含一个bar_t,这是一个没有复制构造函数的类

或operator =。改变bar_t不在我的掌控之内。此外,我需要能够在运行时更新包含的bar_t(因此下面是

set_bar()方法)。


代码*几乎*有效。这是有问题的一行:


void set_bar(bar_t& b){bar = b;}


这无法编译并显示operator =无法访问的消息。为什么

这应该是一个问题因为我正在尝试分配给参考?我只是想要我的引用成员引用一个新对象。我实际上并没有复制

一个对象。运营商为什么要发挥作用?毕竟,我可以将一个

引用传递给bar_t作为参数就好了,即使复制构造函数

也无法访问。


假设虽然我的编译器表现正常,但我无法使用这种方法,无论我是否理解为什么它是'b
不允许。考虑到这一点,我的下一个最佳选择是创建一个类似于以下代码尝试的

效果吗?


谢谢!

Dave


PS如果有人想要询问你想做什么?,bar_t

对应于ofstream而foo_t对应于我的一个应用程序

类。我需要包含一个用于记录的ofstream,我需要能够偶尔更改该流(即开始记录到另一个

的地方)。


class bar_t

{

public:

bar_t(){}


private:

bar_t(const bar_t&); //保留未定义

bar_t& operator =(const bar_t&); //保留未定义

};


class foo_t

{

public:

foo_t():bar(initial_bar){}

void set_bar(bar_t& b){bar = b;}


private :

bar_t initial_bar; //必须来*之前*会员栏,因为它用于

初始化吧。

bar_t& bar;

};


[见 http:// www。 gotw.ca/resources/clcm.htm 了解有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

解决方案

>请考虑以下代码。这是一个问题的代表我

有。

foo_t需要包含一个bar_t,这是一个没有副本的类
构造函数或运算符=。改变bar_t不在我的掌控之内。
此外,我需要能够在运行时更新包含的bar_t(因此下面的
set_bar()方法)。

代码*几乎*工作。这是有问题的一行:

void set_bar(bar_t& b){bar = b;}

无法使用operator =无法访问的消息进行编译。
为什么这是一个问题,因为我正在尝试分配一个参考?我
只希望我的引用成员引用一个新对象;我实际上并不是
复制一个对象。运营商为什么要发挥作用?毕竟,我可以
传递对bar_t的引用作为参数就好了,即使复制
构造函数也是不可访问的。


我认为错误信息让你走错了路。编译器没有抱怨bar_t类的赋值运算符
。真正的问题是,C ++中的C ++引用是不可分配的。他们必须

总是被初始化,以后不能重新安装。

假设我的编译器行为正常,我不能用
来接受这个无论我是否理解为什么不允许这种做法。考虑到这一点,我的下一个最佳替代
会产生类似下面代码尝试的效果吗?




如果你需要吧成员在某个时候引用另一个对象在你的程序中,bar成员必须是一个指针。


[comp.lang.c ++。moderated cross-帖子被删除因为它减慢了

回复]


-

Peter van Merkerk

彼得.van.merkerk(at)dse.nl


blockquote>



Dave写道:

大家好,
foo_t需要包含一个bar_t,这是一个没有复制构造函数的类
或operator =。

void set_bar(bar_t& b){bar = b;}

无法使用operator =无法访问的消息进行编译。为什么这应该是一个问题,因为我正在尝试分配参考?我只希望我的参考成员引用一个新对象;我实际上并不是在复制一个物体。运营商为什么要发挥作用?毕竟,即使复制构造函数
也无法访问,我也可以将
引用传递给bar_t作为参数。


你不能用C ++做到这一点;引用始终引用具有

的对象进行初始化。

考虑到这一点,我的下一个最佳选择是创建
效果类似于下面的代码尝试?




指针可以重新分配以引用不同的对象。


HTH,

杰克

[见 http ://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]




" Dave"其中p是*********** @ yahoo.com>在留言中写道

news:vq ************ @ news.supernews.com ...

大家好,

请考虑以下代码。它代表了我所遇到的一个问题。

foo_t需要包含一个bar_t,它是一个没有复制构造函数的类
或operator =。改变bar_t不在我的掌控之内。此外,
我需要能够在运行时更新包含的bar_t(因此下面的
set_bar()方法)。

代码*几乎*工作。这是有问题的一行:

void set_bar(bar_t& b){bar = b;}

无法使用operator =无法访问的消息进行编译。为什么
这应该是一个问题,因为我正在尝试分配参考?




没有多少分析完成但只有一个想法:

引用是* NOT *指针。引用是别名(如果需要)或

变量的不同名称。你初始化它并使用它像任何其他

变量(使用。不 - >来调用方法)。编译器可能会将

引用转换为指针,但这只是一个实现细节。那么,

是的,你需要一个运营商=


我的2c

/ dan

< snip>


[见 http://www.gotw.ca/resources/clcm.htm 了解有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]


Hello all,

Please consider the code below. It is representative of a problem I am
having.

foo_t needs to contain a bar_t which is a class without a copy constructor
or operator=. It is not within my control to change bar_t. Furthermore, I
need to be able to update the contained bar_t at runtime (hence the
set_bar() method seen below).

The code *almost* works. Here''s the problematic line:

void set_bar(bar_t &b) {bar = b;}

This fails to compile with a message that operator= is inaccessible. Why
should this be a problem since I''m trying to assign to a reference? I only
want my reference member to refer to a new object; I''m not actually copying
an object. Why should operator= come into play? After all, I can pass a
reference to bar_t as a parameter just fine even though the copy constructor
is also inaccessible.

Assuming though that my compiler is behaving properly, I won''t be able to
take this approach regardless of whether or not I understand why it''s
disallowed. With that in mind, what''s my next best alternative to create an
effect similar to what the code below attempts?

Thanks!
Dave

P.S. In case anyone is tempted to ask "What are you trying to do?", bar_t
corresponds to ofstream and foo_t corresponds to one of my application
classes. I need to contain an ofstream for logging, and I need to be able
to change that stream occassionally (i.e. start logging to a different
place).

class bar_t
{
public:
bar_t() {}

private:
bar_t(const bar_t &); // Leave undefined
bar_t &operator=(const bar_t &); // Leave undefined
};

class foo_t
{
public:
foo_t(): bar(initial_bar) {}
void set_bar(bar_t &b) {bar = b;}

private:
bar_t initial_bar; // Must come *before* member bar as it is used to
initialize bar.
bar_t &bar;
};

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

解决方案

> Please consider the code below. It is representative of a problem I
am

having.

foo_t needs to contain a bar_t which is a class without a copy constructor or operator=. It is not within my control to change bar_t. Furthermore, I need to be able to update the contained bar_t at runtime (hence the
set_bar() method seen below).

The code *almost* works. Here''s the problematic line:

void set_bar(bar_t &b) {bar = b;}

This fails to compile with a message that operator= is inaccessible. Why should this be a problem since I''m trying to assign to a reference? I only want my reference member to refer to a new object; I''m not actually copying an object. Why should operator= come into play? After all, I can pass a reference to bar_t as a parameter just fine even though the copy constructor is also inaccessible.
I think the error message put you on the wrong track. The compiler isn''t
complaining about the assignment operator of the bar_t class. The real
problem is that C++ references in C++ are not assignable. They must
always be initialized and cannot be reseated later.
Assuming though that my compiler is behaving properly, I won''t be able to take this approach regardless of whether or not I understand why it''s
disallowed. With that in mind, what''s my next best alternative to create an effect similar to what the code below attempts?



If you need the bar member to reference another object at some point in
your program, the bar member must be a pointer.

[comp.lang.c++.moderated cross-post removed since it slows down
responses]

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl




Dave wrote:

Hello all, foo_t needs to contain a bar_t which is a class without a copy constructor
or operator=.

void set_bar(bar_t &b) {bar = b;}

This fails to compile with a message that operator= is inaccessible. Why
should this be a problem since I''m trying to assign to a reference? I only
want my reference member to refer to a new object; I''m not actually copying
an object. Why should operator= come into play? After all, I can pass a
reference to bar_t as a parameter just fine even though the copy constructor
is also inaccessible.
You cannot do that in C++; a reference always refers to the object with
which it is initialized.
With that in mind, what''s my next best alternative to create an
effect similar to what the code below attempts?



Pointers can be reassigned to refer to different objects.

HTH,
Jack
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]



"Dave" <be***********@yahoo.com> wrote in message
news:vq************@news.supernews.com...

Hello all,

Please consider the code below. It is representative of a problem I am
having.

foo_t needs to contain a bar_t which is a class without a copy constructor
or operator=. It is not within my control to change bar_t. Furthermore, I need to be able to update the contained bar_t at runtime (hence the
set_bar() method seen below).

The code *almost* works. Here''s the problematic line:

void set_bar(bar_t &b) {bar = b;}

This fails to compile with a message that operator= is inaccessible. Why
should this be a problem since I''m trying to assign to a reference?



not much analisys done but one thought:
references are *NOT* pointers. a reference is an alias (if you want) or a
different name for a variable. you initialise it and use it like any other
variable (use . not -> to call methods). The compiler may transform the
reference into a pointer, but this is only an implementation detail. So,
yes, you need an operator=

my 2c

/dan
<snip>

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


这篇关于分配给参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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