Solidity-19 StorageToStorageReferenceTypeAssignment

发布时间:2024年01月21日

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.9;

contract StorageToStorageReferenceTypeAssignment {

? ? // Define two state arrays, stateArray1 and stateArray2, both containing two uint elements.

? ? // stateArray1 initially contains [1, 2], and stateArray2 contains [3, 4].

? ? uint[2] stateArray1 = [uint(1), 2];

? ? uint[2] stateArray2 = [uint(3), 4];

? ? // In the following function:

? ? // - stateArray1 is assigned to be a reference to stateArray2.

? ? // - stateArray2's second element is updated to 5.

? ? // Therefore, stateArray1 now references the same storage location as stateArray2,

? ? // and the value of stateArray1[1] is 5.

? ? function getUInt() public returns (uint) {

? ? ? ? stateArray1 = stateArray2;

? ? ? ? stateArray2[1] = 5;

? ? ? ?

? ? ? ? // Returns the value of stateArray1's second element, which is 5 after the assignment.

? ? ? ? return stateArray1[1]; // returns 5

? ? }

}

//Deploy screenshoot:

文章来源:https://blog.csdn.net/m0_61408373/article/details/135734366
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。