问题标签 [pass-by-value]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
153 浏览

c++11 - 为什么 C++ 标准不禁止这种可怕的用法?

源代码非常简单,不言而喻。问题包含在评论中。

0 投票
3 回答
450 浏览

jquery - jQuery not passing variable after .click

So I have a simple jQuery script running. It has 2 buttons, 1 that sends data, and 1 button needs to set the value of one of the variables send in the first one.

Ok so here is the script for button 1 (called preview):

What this does is put an effect to an image and it all works wonderfull. One of the first lines of the javascript contains the "sourceImage" variable.

That variable is correctly passed to the script above. A second button (called Save State) makes sure that ones I feel happy with the effect of the image I save the state of the image, and continue with effects over that image. This means the source image needs to be changed, which I use the following script for:

I left the line $('#header').html(sourceImage); in to check if it was indeed triggered, and it was, so this line is not neccesary, but I know for sure that the action is triggered.

However, ones I again click on "Preview" in changes the value of variable sourceImage back to the value it started with, while it actually needs to keep this new value.

HOpe there's somebody that can help me.

0 投票
5 回答
498 浏览

c++ - 在这种情况下是值还是引用语义?

对于在某些情况下是选择值还是引用语义,我没有感觉(但我希望如此)。有什么我可以应用的经验法则吗?

我通常会为内置数据类型(char、int、bool、double 等)以外的所有内容选择引用。但是,有时不可能从函数返回引用,所以我不得不使用指针。下面的函数就是一个例子:

我会使用 boost::shared_ptr 来存储 Foo 对象,但这使得使用该对象非常难看。我目前正在研究一个返回一个几乎不会超过 10 个元素的双端队列的函数(这是我的假设,我无法确定)。按值返回可以吗?我的考虑是过早优化的情况吗?

0 投票
5 回答
43717 浏览

c++ - 是否传递指针参数,在 C++ 中按值传递?

是否传递指针参数,在 C++ 中按值传递?因为我看到对指针的任何更改都不会反映在方法之外。我通过取消引用指针所做的更改被反映了。

在这种情况下,使用指向指针的指针作为函数的参数来修改函数内的指针值是否可以接受/标准过程?

0 投票
3 回答
350 浏览

c - 在 C 中传递值

以下代码是否可移植?

我只是按值传递指针,我可以在调用者中更改它!

0 投票
3 回答
167 浏览

c++ - c++函数中的按值返回

这段代码在 C++ 中是否正确?

按值返回列表(的#include <list>)是否会产生问题?

0 投票
3 回答
2202 浏览

c - C99 常量值传递

我一直在研究 GNU Scientific Library 源代码,并且不断看到以下类型的声明:

C99中,声明一个按值传递的参数(例如Nincy在上面的示例中)是否有任何(优化)好处const?无论如何,它们都有一个副本,因为它们是按值传递的,不是吗?

谢谢!内核

0 投票
6 回答
111994 浏览

c - 按值将数组传递给函数

下面是《 C Programming Just the FAQs》一书中的一个片段。这不是错误的,因为数组永远不能按值传递吗?

VIII.6:如何将数组按值传递给函数?

答:数组可以通过值传递给函数,方法是在被调用函数中声明数组名称,并在末尾附加方括号([和)。]调用函数时,只需将数组的地址(即数组的名称)传递给被调用函数即可。例如,以下程序将数组传递 x[]给按值命名的函数 byval_func()

int[]参数告诉编译器该byval_func() 函数将采用一个参数——一个整数数组。调用该 byval_func()函数时,您将数组的地址传递给 byval_func()

因为数组是按值传递的,所以会制作数组的精确副本并将其放置在堆栈上。然后被调用的函数接收这个数组的副本并可以打印它。因为传递给的数组 byval_func()是原始数组的副本,所以在byval_func()函数内修改数组对原始数组没有影响。

0 投票
5 回答
20107 浏览

c++ - 使用指针传递引用和值

我不明白为什么传递指向函数的指针不会改变传入的数据。如果函数原型看起来像这样:

而func给p分配了内存,为什么不能在函数外使用呢?我以为指针是地址?

0 投票
2 回答
4897 浏览

vba - ByRef 似乎收到了价值,而不是 VBA 6.0 中的参考

我的小示例代码

应该输出 3 但输出 1。更具体地说,ana在调用AddNr函数后不会修改变量。

我的环境是 Excel 2007 中的 Microsoft Visual Basic 6.5。