我创建2个变量的名称与一个和b然后我初始化变量一个有值"开始",后来我给你变一个变量b,最后我改变变量的值,一到年底,但变量b不改变,而一个和b是参考类型!
string a = "start"; // Declare and initialize a variable
string b = a; // Copy the variable's value to a new variable
a = "end"; // Modify the value of the original variable
//variable b don't change!!
Run Code Online (Sandbox Code Playgroud)
为什么这不按预期工作?
例如,我有以下代码:
int a = 1;
int b = 2;
int *c = &a;
int &d = *c;
c = &b;
d++;
Run Code Online (Sandbox Code Playgroud)
第 4 行的行为是什么?如果我想要一个指针的引用,使用它是否正确
int *&e = c;
Run Code Online (Sandbox Code Playgroud)
有什么理由选择对指针的引用吗?
在我的理解array中int array[]={1,2,3,4,5}只是一个指向第一个元素的指针array.这意味着array可以将其分配给ptr类型的指针int*.
参数int* &pin hoo将通过引用传递参数.这意味着我们可以将传递的参数更改为指向其中的另一个值hoo.
void hoo(int* &p, int n)
{
for (int i = 0; i < n; i++)
cout << p[i] << endl;
}
int main()
{
int array[] = { 1,2,3,4,5 };
// I can do this
int* ptr = array;
hoo(ptr, 5);
// but not this.
//hoo(array, 5);
}
Run Code Online (Sandbox Code Playgroud)
为什么我们不能将int传递array给hoo没有ptr?
我有一个JavaScript/ES6 class,其成员是一个名为x,初始化为的数组[1,2,3,4].
当我在方法中声明一个新变量,调用y并赋值x给它,然后更改其中的值时x,y保持不变,表明它y是一个副本x.
如果我声明并分配y给z在同一方法中调用的变量,则修改z更改y,但不会更改x.
这表明声明一个类级别数组(对象?)然后将其分配给方法内的变量会复制该对象.这与C#等语言有很大不同.
为什么在JavaScript/ES6中以这种方式实现?
class Alpha {
constructor() {
this.x = [1, 2, 3, 4];
}
DoSomething() {
console.log('x is ' + this.x); // x is 1,2,3,4
let y = this.x;
this.x = [99, 99, 99, 99];
console.log('x is ' + this.x); // x is 99,99,99,99
console.log('y is ' + y); …Run Code Online (Sandbox Code Playgroud)javascript reference pass-by-reference variable-assignment ecmascript-6
代码的输出是30。但是我不确定它是如何实现的。
#include <iostream>
int &fun()
{
static int x = 10;
return x;
}
int main()
{
fun() = 30;
std::cout << fun();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我期望输出为10,但显示为30。如何?
基本上我想要实现的是使用指针,避免new关键字来获得与其他语言相同的复制行为(=如果我们谈论对象,运算符会将右操作数的 ref 分配给左操作数,类似于shared_ptr应该提供)。如果我不使用数组运算符,我要展示的内容似乎有效[]。为什么会发生这种情况,我该如何解决?
// Declaration way before
std::vector<int>* test;
test = &std::vector<int>();
(*test)[1] = 0;
Run Code Online (Sandbox Code Playgroud)
// Declaration way before
std::map<std::string, int>* test;
test = &std::map<std::string, int>();
(*test)["a"] = 0;
Run Code Online (Sandbox Code Playgroud)
错误发生在两者的最后一行。
C++ 中的代码
#include <iostream>
#include <string>
using namespace std;
int main() {
string food = "Burger";
cout << &food << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
用 Rust 编写的相同代码:
fn main() {
let food = "Burger";
prinln!("{}", &food);
}
Run Code Online (Sandbox Code Playgroud)
C++ 程序的输出:
0x7fff604b2890
Run Code Online (Sandbox Code Playgroud)
Rust 程序的输出:
Burger
Run Code Online (Sandbox Code Playgroud)
为什么会这样?我错过了什么?
我正在阅读C++ Primer,当我读到以下内容时,它真的让我很困惑:
int odd[] = {1, 3, 5, 7, 9};
int even[] = {0, 2, 4, 6, 8};
//return a pointer, which points an array with 5 int
decltype(odd) *arrPtr(int i)
{
return (i % 2) ? &odd : &even;
}
int main()
{
cout << "odd: " << odd << endl;
cout << "even: " << even << endl;
cout << arrPtr(3);
}
Run Code Online (Sandbox Code Playgroud)
结果是:
odd: 0x472010
even: 0x472030
0x472010
Run Code Online (Sandbox Code Playgroud)
让我困惑的是为什么需要&。我的意思是,有必要添加“&”吗?
我尝试像这样删除&:
odd: 0x472010
even: 0x472030
0x472010
Run Code Online (Sandbox Code Playgroud)
但它就是行不通: …