gpt4 book ai didi

Why can we GET the value of an object within an object, but not SET its value?(为什么我们可以获得对象中对象的值,但不能设置它的值?)

转载 作者:bug小助手 更新时间:2023-10-25 23:31:31 25 4
gpt4 key购买 nike



In the below code, I have created a very basic template class Pair and its 2 objects - p1 & p2.

I can get the value of data member x of object p2 using cout << p2.getx().getx() << p2.getx().gety();.

在下面的代码中,我创建了一个非常基本的模板类对及其2个对象-p1和p2。我可以使用cout<


Main Question:


If I can GET the value in this way, then why cannot I SET the value of x using
(p2.getx()).setx('c');
and
(p2.getx()).sety(8.12); ?

如果我可以用这种方式获得值,那么为什么不能使用(p2.getx()).setx(‘c’);和(p2.getx()).sty(8.12);来设置x的值呢?


Code:


#include<iostream>
using namespace std;

template <typename T, typename V> class Pair {
T x;
V y;

public:
void setx(T x) {
this->x = x;
}
T getx() {
return x;
}
void sety(V y) {
this->y = y;
}
V gety() {
return y;
}
};

int main()
{
Pair <char,int> p1; //Declared to put in p2
p1.setx('b');
p1.sety(12.23);

Pair <Pair<char,int>, double> p2;
p2.setx(p1);
p2.sety(24.36);

cout<<p2.getx().getx()<<" "<<p2.getx().gety()<<" "<<p2.gety()<<endl;
(p2.getx()).setx('c'); //Here, p2.getx()!=p1, even though we set p2.x=p1. This is because p2.getx() returns a value/copy of p1 (an instance), not an object (or var).
(p2.getx()).sety(8.12);
p2.setx(p1);
cout<<p2.getx().getx()<<" "<<p2.getx().gety()<<" "<<p2.gety()<<endl;
return 0;
}

What I think I've understood so far is that p2.getx() returns a copy of p1, and to make p2.getx().setx(value) work, I would have to make it return a reference to p1.

But still what I would like to understand is that if p2.getx().𝒔𝒆𝒕𝒙(𝒗𝒂𝒍𝒖𝒆) doesn't work, how does p2.getx().𝒈𝒆𝒕𝒙() still work?

我认为到目前为止我已经理解的是,p2.getx()返回p1的副本,要使p2.getx().setx(值)工作,我必须让它返回对p1的引用。但我仍然想了解的是,如果p2.getx().𝒔𝒆𝒕𝒙(𝒗𝒂𝒍𝒖𝒆)不工作,p2.getx().𝒈𝒆𝒕𝒙()如何工作呢?


更多回答

What I think I've understood so far is that p2.getx() returns a copy of p1, and to make p2.getx().setx(value) work, I would have to make it return a reference to p1. Correct. ut still what I would like to understand is that if p2.getx().𝒔𝒆𝒕𝒙(𝒗𝒂𝒍𝒖𝒆) doesn't work, how does p2.getx().𝒈𝒆𝒕𝒙(𝒗𝒂𝒍𝒖𝒆) still work? You get the x of the copy, why shouldn't this work?

我认为到目前为止我已经理解的是,p2.getx()返回p1的副本,要使p2.getx().setx(值)工作,我必须让它返回对p1的引用。对,是这样。但我仍然想了解的是,如果p2.getx().𝒔𝒆𝒕𝒙(𝒗𝒂𝒍𝒖𝒆)不起作用,p2.getx().𝒈𝒆𝒕𝒙(𝒗𝒂𝒍𝒖𝒆)怎么还能起作用?你得到了副本的x,为什么这个不能用呢?

@tkausl So does this mean that p2.getx().𝒔𝒆𝒕𝒙(𝒗𝒂𝒍𝒖𝒆) is working in the background but there is no way to see it since it is changing the value of p1's copy? If it is so then is there a way to see the change in p1's copy?

@tkaul那么这是否意味着p2.getx().𝒔𝒆𝒕𝒙(𝒗𝒂𝒍𝒖𝒆)正在后台工作,但无法看到它,因为它正在更改p1‘S副本的值?如果是这样的话,有没有办法看到p1‘S文案的变化?

So does this mean that p2.getx().𝒔𝒆𝒕𝒙(𝒗𝒂𝒍𝒖𝒆) is working in the background but there is no way to see it since it is changing the value of p1's copy? Yes. If it is so then is there a way to see the change in p1's copy? It ceases to exist at the next semicolon.

那么,这是否意味着p2.getx().𝒔𝒆𝒕𝒙(𝒗𝒂𝒍𝒖𝒆)正在后台工作,但无法看到它,因为它正在更改p1‘S副本的值?是。如果是这样的话,有没有办法看到p1‘S文案的变化?它在下一个分号处不复存在。

@tkausl Okayy I got it. Should I delete the question now? Cuz it seems stupid now🥲

@tkaul好的,我知道了。我现在是否应该删除该问题?因为现在看起来很愚蠢,🥲

优秀答案推荐

The p2.getx().𝒈𝒆𝒕𝒙(𝒗𝒂𝒍𝒖𝒆) works because you're returning a copy.
Let's say that I have a function that accept an integer value by copy int functionX(int x) and increments the value that it receives with ++x and I'm passing an int value_int = 50 to the function.
The body of functionX will be able to read x as 50 because it's a copy of value_int. If you runstd::cout << functionX and thenstd::cout << value_int you'll find out that the first line will print 51 and the second one 50, that basically means that passing by copy creats problems only with the writing of an object not the reading. If you want to change values of an object inside a function you should pass it by reference and in your example the first function should return a reference because your're chaining two calls

P2.getx().𝒈𝒆𝒕𝒙(𝒗𝒂𝒍𝒖𝒆)可以工作,因为您要返回一个副本。假设我有一个函数,它通过复制int函数X(Intx)接受一个整数值,并用++x递增它接收到的值,我将一个int value_int=50传递给该函数。函数X的主体将能够将x读取为50,因为它是Value_int的副本。如果运行std::cout<



For your main question, your getx() method returns a copy of x inside p2, if you set value to this copy, will not affect the x inside p2. If you want to change the x inside p2, your getx() method should return a reference like this:

对于您的主要问题,您的getx()方法返回p2内的x的副本,如果您为该副本设置值,则不会影响p2内的x。如果要更改p2中的x,则getx()方法应返回如下引用:


T& getx() { return x; }

For this question: "But still what I would like to understand is that if p2.getx().𝒔𝒆𝒕𝒙(𝒗𝒂𝒍𝒖𝒆) doesn't work, how does p2.getx().𝒈𝒆𝒕𝒙() still work?"

对于这个问题:“但我仍然想了解的是,如果p2.getx().𝒔𝒆𝒕𝒙(𝒗𝒂𝒍𝒖𝒆)不工作,p2.getx().𝒈𝒆𝒕𝒙()怎么还能工作呢?”


p2.getx().𝒔𝒆𝒕𝒙(𝒗𝒂𝒍𝒖𝒆) doesn't work because you didn't set the x inside p2 directly as I explain before. p2.getx().𝒈𝒆𝒕𝒙() works because the first getx() returns a copy of x inside p2, and the second getx() will call the getx() of the copy, so it works.

P2.getx().𝒔𝒆𝒕𝒙(𝒗𝒂𝒍𝒖𝒆)不起作用,因为您没有像我前面解释的那样直接在p2中设置x。P2.getx().𝒈𝒆𝒕𝒙()之所以起作用,是因为第一个getx()返回了p2内的x的一个副本,而第二个getx()将调用该副本的getx(),所以它起作用了。


Hope I clarify clearly.

希望我能说清楚。


更多回答

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

正如它目前所写的,你的答案并不清楚。请编辑以添加更多详细信息,以帮助其他人了解这是如何解决提出的问题的。你可以在帮助中心找到更多关于如何写出好答案的信息。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com