引用问题 [英] Problem with references

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

问题描述




在课程AI中获得''SomeType''的引用:


A级{

SomeType测试;

公开:

A(SomeType& someT){

test = someT;


}

...

...

};


但是如果我在测试中做的操作在someT实例中不可见

因为test是本地副本。但是我该如何测试一个参考副本

of someT所以当我改变测试时我也改变了一些?

解决方案

meekee写道:


在课程AI中获得对''SomeType''的引用:


class A {

SomeType测试;


公开:

A(SomeType& someT){

test = SOMET;



不要分配。初始化。


>

}

..

..

};


但如果我在测试中进行操作,则在someT

实例中不可见,因为test是本地副本。但是我如何进行测试

参考副本? someT所以当我改变测试时我也改变了一些?



让你的''test''成员成为SomeType的引用,并在初始化列表中初始化它为

。你对''test''所做的所有chagnes都将在''someT'所引用的对象中显示为




V

-

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

我不回复热门帖子回复,请不要问


Victor Bazarov写道:


meekee写道:
< blockquote class =post_quotes>
>在类AI中获得对''SomeType''的引用:

A类{
SomeType测试;

public:
A(SomeType& someT){
test = someT;



不要分配。初始化。


>}
..
..
};

但如果我因为test是本地副本,所以在someT
实例中看不到测试操作。但是我该如何进行测试?参考副本 someT所以当我改变测试时我也改变了一些?



让你的''test''成员成为SomeType的引用,并在初始化列表中初始化它为
。你对''test''所做的所有chagnes都将在''someT'所引用的对象中显示为




V



我试过让测试成员引用SomeType:


SomeType&测试;


然后我得到一个错误,禁止它初始化字段。如何使用

构造函数中首次出现的参数初始化一个字段成员?


On Sep 27,下午2:32,meekee< mee ... @dd.comwrote:


在课程AI中获得对''SomeType''的引用:


A级{



// SomeType测试;


SomeType&测试;


>

public:

A(SomeType& someT){

test = someT;


}

..

..


};


但是如果我在测试中进行操作,则在someT实例中看不到

,因为test是本地副本。但是我该如何测试某个参考副本某些东西的b $ b,所以当我改变测试时我也会改变某些东西?





In class A I get a reference to ''SomeType'':

class A {
SomeType test;
public:
A(SomeType& someT) {
test = someT;

}
...
...
};

But if I do operations on test the are not visible in the someT instance
since test is a local copy. But how do I make test a "reference copy"
of someT so when I change test I also change someT?

解决方案

meekee wrote:

In class A I get a reference to ''SomeType'':

class A {
SomeType test;
public:
A(SomeType& someT) {
test = someT;

Don''t assign. Initialise.

>
}
..
..
};

But if I do operations on test the are not visible in the someT
instance since test is a local copy. But how do I make test a
"reference copy" of someT so when I change test I also change someT?

Let your ''test'' member be a reference to SomeType, and initialise it
in the initialiser list. The all chagnes you make to ''test'' will be
visible in the object referred to by ''someT'', as well.

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


Victor Bazarov wrote:

meekee wrote:

>In class A I get a reference to ''SomeType'':

class A {
SomeType test;
public:
A(SomeType& someT) {
test = someT;


Don''t assign. Initialise.

>}
..
..
};

But if I do operations on test the are not visible in the someT
instance since test is a local copy. But how do I make test a
"reference copy" of someT so when I change test I also change someT?


Let your ''test'' member be a reference to SomeType, and initialise it
in the initialiser list. The all chagnes you make to ''test'' will be
visible in the object referred to by ''someT'', as well.

V


I have tried making the test member a reference to SomeType:

SomeType& test;

but then I get an error that its forbidden to initialize the field. How
do I initialize a field member with an argument that first available in
the constructor?


On Sep 27, 2:32 pm, meekee <mee...@dd.comwrote:

In class A I get a reference to ''SomeType'':

class A {

//SomeType test;

SomeType& test;

>
public:
A(SomeType& someT) {
test = someT;

}
..
..

};

But if I do operations on test the are not visible in the someT instance
since test is a local copy. But how do I make test a "reference copy"
of someT so when I change test I also change someT?



这篇关于引用问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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