标签: pass-by-value

C++ Pass by Reference程序

IBM在下面的示例中解释了C++通过引用传递(包括源代码).

如果我换void swapnum...void swapnum(int i, int j),它会变成价值吗?

// pass by reference example
// author - ibm

#include <stdio.h>

void swapnum(int &i, int &j) {
  int temp = i;
  i = j;
  j = temp;
}

int main(void) {
  int a = 10;
  int b = 20;

  swapnum(a, b);
  printf("A is %d and B is %d\n", a, b);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

资源

c++ pass-by-reference pass-by-value

0
推荐指数
1
解决办法
4583
查看次数

C++指针问题

我有一个我似乎无法弄清楚的指针问题.看起来我已经用这种方式使用指针了1000次,所以我不太清楚这里发生了什么.我有以下代码:

int iRetVal;
CycleCountOrder* cycleOrder =  NULL;
CycleCountLineItem* cycleLine = NULL;


iRetVal = m_CycleCount.GetCCOrderLine(pOneLocation.szOrderNum[c], cycleOrder, cycleLine);
Run Code Online (Sandbox Code Playgroud)

每当我调用GetCCOrderLine时,我都会进入函数内部,并为指针cycleOrder和cycleLine分配有效值.当我走出函数GetCCOrderLine之外时,引用再次为NULL.下面的代码是如何定义GetCCOrderLine:

头文件

int GetCCOrderLine(CString szOrderLnitem, CycleCountOrder* cycleOrder, CycleCountLineItem* cycleCountLine);
Run Code Online (Sandbox Code Playgroud)

cpp文件

int CCycleCount::GetCCOrderLine(CString szOrderLnitem, CycleCountOrder* cycleOrder, CycleCountLineItem* cycleCountLine)
{
    CString szCurrOrderLnitem;
    for(int c = 0; c < m_Orders.GetCount(); c++)
    {
        CycleCountOrder* currentOrder = m_Orders[c];

        for(int d = 0; d < currentOrder->m_LineItems.GetCount(); d++)
        {
            CycleCountLineItem* currentLine = currentOrder->m_LineItems[d];

            szCurrOrderLnitem.Format("%s-%d-%d", currentOrder->szOrderNum, currentLine->nLnitemNum, currentLine->nSubitemNum);

            if(szCurrOrderLnitem == szOrderLnitem)
            {
                cycleOrder = currentOrder;
                cycleCountLine = currentLine;
                return FUNC_OK;
            }
        } …
Run Code Online (Sandbox Code Playgroud)

c++ pointers pass-by-reference pass-by-value

0
推荐指数
1
解决办法
198
查看次数

将对象传递给java中的方法似乎是通过引用(而Java是由val)

我认为当你将对象传递给Java中的方法时,它们应该是有价值的.

这是我的代码:

public class MyClass{
    int mRows;
    int mCols;
    Tile mTiles[][];     //Custom class

    //Constructor
    public MyClass(Tile[][] tiles, int rows, int cols) {
        mRows = rows;
        mCols = cols;

        mTiles = new Tile[mRows][mCols];
        for (int i=0; i < mRows; i++) {
            for (int j=0; j < mCols; j++) {
                mTiles[i][j] = new Tile();
                mTiles[i][j] = tiles[i][j];
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

此时,mTiles对象的任何更改都会反映回tiles对象.任何想法如何解决这一问题?

谢谢大家.

java eclipse android pass-by-reference pass-by-value

0
推荐指数
1
解决办法
2816
查看次数

java:按值传递或按引用传递

我有两个代码片段:

第一

class PassByTest{
    public static void main(String... args){
        PassByTest pbt=new PassByTest();
        int x=10;
        System.out.println("x= "+x);
        pbt.incr(x);//x is passed for increment
        System.out.println("x= "+x);//x is unaffected
    }
    public void incr(int x){
        x+=1;
    }
}
Run Code Online (Sandbox Code Playgroud)

在此代码中,值x不受影响.

第二

import java.io.*;
class PassByteTest{
    public static void main(String...args) throws IOException{
        FileInputStream fis=new FileInputStream(args[0]);
        byte[] b=new byte[fis.available()];
        fis.read(b);//how all the content is available in this byte[]?

        for(int i=0;i<b.length;i++){
            System.out.print((char)b[i]+"");
            if(b[i]==32)
                System.out.println();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里,文件的所有内容都可以在byte[] b.
怎么样,为什么?

java pass-by-reference pass-by-value

0
推荐指数
1
解决办法
5886
查看次数

了解变量的传递方式

我只是无法理解变量是如何传递的,为什么有些是通过引用传递而另一些是通过值传递的?

例:

var a=4;
var b=a;
b=b++;
alert(a);//unmodified 4

var c=["t","ttt"];
var d=c;
d=d.sort(function(x,y){return (y.length-x.length);});
alert(c);//modified ["ttt","t"]
Run Code Online (Sandbox Code Playgroud)

我在哪里可以找到哪些变量可以像第一个例子一样工作,哪个像第二个?(布尔,字符串等...有太多的东西可以通过恶误来测试它们)

javascript variables pass-by-reference pass-by-value

0
推荐指数
1
解决办法
132
查看次数

为什么在这个例子中,按值传递有效?

这个例子取自Java编程书; 第4版.

在绊倒这个代码示例后,我很困惑,为什么它仍然打印出对象的新引用,即使我们声明它为'null'.理论上,我们正在改变对整个程序共享的对象的引用,尽管我们在commonName方法中将对象初始化为null.在commonName的控制流程点,body构造函数中的字段被初始化为"Sirius"; 当我们改变对象的引用时(在Java中,你可以按值调用),该字段将更改为Dog Star.我们将整个对象设置为null的方法的最后一行,一旦我们打印出对象,运行时应该使用空引用来问候我们.

解决这个问题的唯一方法是将commonName方法设置为final.任何Java专家都可以解释为什么会发生这种情况,特别是在任何按值调用的语言中.

class PassRef
{
    public static void main (String[] args) {
        Body sirius = new Body ("Sirus", null);
        System.out.println ("before: " + sirius);
        commonName(sirius);
        System.out.println("after: " + sirius);
    }

    public static void commonName (Body bodyref) {
        bodyref.name = "Dog Star";
        bodyref = null;
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

before: 0 (Sirius)
after:  0 (Dog Star)
Run Code Online (Sandbox Code Playgroud)

java overloading pass-by-value

0
推荐指数
1
解决办法
148
查看次数

理解Java按值传递

我经历了stackoverflow中的几个帖子,并搜索其他好的网站,以了解java传递值的概念..

我怀疑如下.

Dog someDog = new Dog("FOO"); //mem location 42
foo(someDog);
someDog.setName("FIFI"); // false

    foo(Dog newDog){
      newDog.name.equals("FOO"); //true
      newDog = new Dog("FIFI");  // create a new DOG object mem location 72. my doubt here
      newDog.name.equals("FIFI"); //true
    }
Run Code Online (Sandbox Code Playgroud)

如上所述,newDog = new Dog("FIFI");我的理解是新的DOG对象在mem位置72处创建并分配给mem位置42处的另一个Dog位置对象.

这是什么意思?在背景..

关心Punith.

java pass-by-reference pass-by-value

0
推荐指数
1
解决办法
169
查看次数

这些变化会反映在原始变量中吗?

在下图中,p指向person.

p作为对名为的函数的引用传递callFunction.现在,如果我对p意志做出任何改变,他们也会反映出来person吗?请解释.

我理解传递的参数是原始变量的参考值(我希望如此!).但我无法进一步思考.

在此输入图像描述

java oop parameter-passing pass-by-reference pass-by-value

0
推荐指数
1
解决办法
625
查看次数

Java对象作为函数参数传递

以下关于Java的声明是假的吗?

在Java中,当一个类或对象的实例被指定为方法的参数时,正在创建该对象的副本

我知道Java中的函数是按值传递的,这意味着正在制作对象的副本?

但与此同时,如果java对象是引用并且你传递了一个引用,这不同于实际数据的副本不是吗?

如果你传递一个引用,当重新分配引用时,对象将被重新分配,使得Java传递引用不是按值传递的?

如你所见,我对此非常困惑

java function object pass-by-reference pass-by-value

0
推荐指数
1
解决办法
1918
查看次数

在lisp中按值传递参数

我遇到的问题是我无法解决lisp按值而不是通过引用传递数组的事实.

当我研究这个问题时,其他人的问题的解决方案分为四类.

  1. 使用一个闭包
  2. 从功能上思考
  3. 使用宏
  4. 使用全局变量,即defparameter

1&2)我不知道前两个解决方案是如何使用的.也许我不明白闭包是如何工作的或如何在功能上思考.

3)我已经创建了宏而不是函数,但是当函数自行递归时它们不起作用(除非它们确实有效并且我只是不正确地编写它们).

4)我不喜欢全局的想法.

我在写的一些函数中遇到了这个问题.我最成功的是在函数返回时返回数组.但是当函数在数组的子序列上递归时,我就被卡住了.

我想要一个通用的解决方案,我如何以不同的方式思考这些问题,而不是解决这里提交的代码.

如果你想引用我试图编写的特定代码片段,这里就是.

我试图在Lisp中为HackerRank编写一些代码.

我需要一个eratosthenes筛子(获得素数).

我写了以下内容,由于上述原因不起作用.(我的逻辑也可能是错的.)

;sieve-numbers is an array with :initial-element nil
;sieve-numbers represents only the odd numbers starting at 3
;i.e., (aref sieve-numbers n) represents the value 3+n+n
;when the sieve is done (aref sieve-numbers n) is nil if (+ 3 n n) is prime
;removed is the number of values in the sieve-numbers array that have been chopped off the front

(defun sieve-knock (sieve-numbers n)
    "knock out …
Run Code Online (Sandbox Code Playgroud)

lisp parameters common-lisp pass-by-value

0
推荐指数
1
解决办法
356
查看次数