简体   繁体   English

我将一个变量分配给存储在指针地址中的值,那么为什么当初始值改变时它不改变呢?

[英]I assigned a variable to the value of what is stored at a pointer's address, so why doesn't it change when the initial value does?

Basically, I just want to know why 'C' doesn't change when 'A' does.基本上,我只想知道为什么“A”发生变化时“C”不会改变。 I'm guessing that it's because of the hierarchy, but I wanted to confirm and get an explanation as to why...我猜这是因为等级制度,但我想确认并解释为什么......

#include <iostream>
using namespace std;

class IntCell
{
public:
  explicit IntCell( int initialValue = 0 )
  { storedValue = new int{ initialValue }; }
  
  int read() const
  { return *storedValue; }
  
  void write( int x )
  { *storedValue = x; }
  
private:
  int *storedValue;
};

int main() {
  IntCell a{ 2 };
  IntCell* b = &a;
  IntCell c;
  
  int x = 10000;
  int* y = &x;
  
  cout << *y << endl;
  
  c = *b;
  
  a.write(4);
  cout << a.read() << endl << c.read();
  return 0;
}

I test your code and when you change "a", "c" change too.我测试你的代码,当你改变“a”时,“c”也会改变。 Also You need to implement a copy constructor and assignment operator.您还需要实现一个复制构造函数和赋值运算符。

IntCell c; // you create a new instance with new memory address.

在此处输入图像描述

c = *b; // now this instance point to same memory as "a".

在此处输入图像描述

a.write(4); // so when you change "a", "c" change too.

在此处输入图像描述

  1. In your case, the destructor is missing.在您的情况下,缺少析构函数。

     ~IntCell(){ cout<<"deleting...\n"; delete storedValue; }
  2. You are using the implicitly-defined copy assignment.您正在使用隐式定义的复制分配。 That is why c = *b;这就是为什么c = *b; works.作品。 After the copy assignment, IntCell c and IntCell a both have the member which points to the same location.复制分配后, IntCell cIntCell a都有指向同一位置的成员。 If a.write(4) , c.read() will get the same value 4 .如果a.write(4)c.read()将获得相同的值4

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C ++中分配给float变量时,为什么双引用值不会改变 - Why double reference value doesn't change when assigned to float variable in C++ 为什么更改指针的引用不会改变它的原始值? - Why changing the reference of a pointer doesn't change it's original value? 当指向动态分配的内存时,指针的指针将分配什么值? - What value does a pointer to pointer get assigned when points to a dynamically allocated memory? 为什么我调用 function 时指针会改变地址? - Why does a pointer change address when I call a function? 为什么lambda取变量的初始值 - Why lambda takes variable's initial value 为什么不能将具有返回值的函数分配给具有void的指针函数? - Why can't a function with return value be assigned to a pointer function with void? 如何将数组传递给函数,以便在更改该函数中的数组值时原始数组的值不会改变? - How to pass an array to a function so that the original array's value doesn't change when changing the array's values in that function? 为什么我不能在循环中更改指针的内容? - Why can't I change the pointer's content when in a loop? 访问指针指针指向的地址中存储的值(即地址)是否返回垃圾值? - Accessing value(which is an address) stored in an address pointed by pointer variable returns garbage value? 为什么在函数完成执行后释放局部变量的值(其地址存储在全局变量中) - Why value of local variable(its address is stored in global variable) is freed when function has completed its execution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM