在C++;Java中的vs

在C++;Java中的vs,java,c++,pass-by-reference,pass-by-value,Java,C++,Pass By Reference,Pass By Value,如果你能给我一个答案,我将很高兴。我知道java只是值> ,C++为>强>值> ,但也引用> 。我将在下面发布一些代码,以便您理解我的困惑 >强> >传递值< /强> C++: void modify(int x, int y){ x=10; y=20; } void main(){ int a=5,b=8; cout << a; //outputs 5 cout << b; //outputs 8 modify(a,b); cout <

如果你能给我一个答案,我将很高兴。我知道java只是<强>值> <强>,C++为>强>值> <强>,但也<强>引用> <强>。我将在下面发布一些代码,以便您理解我的困惑

<> >强> >传递值< /强> C++:

void modify(int x, int y){
  x=10;
  y=20;
}
void main(){
  int a=5,b=8;
  cout << a; //outputs 5
  cout << b; //outputs 8
  modify(a,b);
  cout << a; //still outputs 5
  cout << b; //still outputs 8
}

从我注意到的,在C++的情况下,我们把“A”的值从5改为10,在爪哇,通过将狗的名字从漫游者设置为马克斯,它也不是强>引用的引用< /强>? 看看这句话:

/*If Java had pass-by-reference semantics, the foo method we defined above would have changed where myDog was pointing when it assigned someDog on line BBB.*/

我也在C++中应用了前面的例子:

void modify(int* p) {
    *p = 10;
    int b = 20;
    p = &b;
}
void main()
{
    int a = 5;
    int* p = &a;
    cout << p << endl; //OUTPUTS memory address 2002(e.g. let's say)
    modify(p);
    cout << a << endl; //a is now 10.
    cout << p << endl; // ALSO OUTPUTS the same memory address 2002
}
void修改(int*p){
*p=10;
int b=20;
p=&b;
}
void main()
{
INTA=5;
int*p=&a;

当谈论“路过任何东西”时,最大的问题是什么不同的人对这些术语有不同的定义。因此,为了回答这个问题,我将首先给出“按值传递”和“按引用传递”的定义。如果直接赋值到函数中的参数对调用范围中传递的变量没有影响,则函数参数就是按值传递的。函数如果对函数内参数的直接赋值与对调用范围内传递的变量的直接赋值具有相同的效果,则ion参数是通过引用传递的

在这个定义下,java(以及许多其他语言,如C、JavaScript、Python、Ruby、Stand)总是按值传递。在这个定义下,C++函数在函数参数具有<代码>和>代码>时总是通过值(包括这里所给出的C++示例)当函数参数没有<代码>和代码>时,值总是通过值。第二个C++示例是通过值,因为如果在代码< > MULTFYYY()/代码>内直接分配给<代码> P<代码>(即<代码> P=;;< /代码>),它对调用范围没有影响。

,您从未直接分配给
p
;而是使用
*
运算符取消引用
p
,并将其分配给结果。这与分配给
p
不同

< C++和java之间最大的区别是什么类型可用。java中的唯一类型是基元类型和引用类型。引用类型的值是“引用”,即指向对象的指针。没有“对象类型”。,因此变量的值不能直接是对象。没有指向原语的指针、指向指向对象的指针的指针或其他指针组合

您的Java示例:

public static void main(String[] args) {
    Dog myDog = new Dog("Rover");
    foo(myDog);
    // here, "myDog" points to the first Dog object, and has the name "Max"
}

public static void foo(Dog someDog) {
    someDog.setName("Max");
    someDog = new Dog("Fifi");
    someDog.setName("Rowlf");
}

相当于这个C++代码(忽略内存管理方面):

这两个示例都是按值传递的,因为它们都直接分配给参数(
someDog=
),并且在这两种情况下,它们对调用范围没有影响——它们都只是更改函数中的指针以指向不同的对象,但不影响
main
中的指针指向的对象。(注意语法有点不同:java中的<>代码>狗< /C> >是C++中的<代码>狗*/COD>类型,C++中的“代码> > ->代码>等价于<代码> .<代码>在爪哇。 C++具有对象类型,其中变量的值可以是对象本身。在这种情况下,此类型的参数表示在传递时复制对象:

void main(void) {
    Dog myDog("Rover");
    foo(myDog);
    // here, the Dog object in the local variable "myDog" has the name "Fifi"
}

void foo(Dog someDog) {
    // this calls the object's assignment operator, which by default
    // copies all the fields
    someDog = Dog("Fifi");
}
Java中没有与之等效的对象,因为Java没有对象类型。对象总是通过引用(指向对象的指针)进行操作

也可以通过C++中的引用传递一个对象,如:

void main(void) {
    Dog myDog("Rover");
    foo(myDog);
    // here, the Dog object in the local variable "myDog" has the name "Fifi"
}

void foo(Dog &someDog) {
    // this calls the object's assignment operator, which by default
    // copies all the fields
    someDog = Dog("Fifi");
}
在这里,对参数(
someDog=
)的直接赋值与调用范围中的相同赋值具有相同的效果。Java没有等价物,因为它既没有对象类型,也没有按引用传递

您还可以通过引用传递指向对象的指针:

void main(void) {
    Dog *myDog = new Dog("Rover");
    foo(myDog);
    // here, "myDog" points to the second Dog object, and has the name "Fifi"
    // the first Dog object still has the name "Rover"
}

void foo(Dog *&someDog) {
    someDog = new Dog("Fifi");
}
这里,直接赋值给参数(
someDog=
)与调用作用域中的同一赋值具有相同的效果,这会导致指针指向不同的
Dog
对象。Java没有等价物,因为它没有按引用传递。但是,有些语言的类型与Java相似,但有按引用传递。例如,如果参数ter标记为
ref
out
,如果参数标记为
&
,则PHP通过引用传递。以下是C中的等效代码:


注意C++中的指针而不是引用。要通过引用,必须使用<代码>无效FUNC(INT/REF)。相关的有趣事实:在<代码>空洞修改(int *p)中java C++ C++术语java中的亚历克斯是对象的指针,并通过基本数据类型的值传递。这就是为什么Java可以抛出一个Null PoExtExchange。我认为Ejayy是正确的,它主要是术语问题。java和C++是不同的。不同的语言和术语有着不同的历史和传统。Java是按价值传递的,因为这是James Gosling所说的。理性的人可能会对术语和事物的专有名称产生分歧。Java引用确实有按引用传递的语义,不管它们使用什么术语。
void main(void) {
    Dog myDog("Rover");
    foo(myDog);
    // here, the Dog object in the local variable "myDog" has the name "Fifi"
}

void foo(Dog someDog) {
    // this calls the object's assignment operator, which by default
    // copies all the fields
    someDog = Dog("Fifi");
}
void main(void) {
    Dog myDog("Rover");
    foo(myDog);
    // here, the Dog object in the local variable "myDog" has the name "Fifi"
}

void foo(Dog &someDog) {
    // this calls the object's assignment operator, which by default
    // copies all the fields
    someDog = Dog("Fifi");
}
void main(void) {
    Dog *myDog = new Dog("Rover");
    foo(myDog);
    // here, "myDog" points to the second Dog object, and has the name "Fifi"
    // the first Dog object still has the name "Rover"
}

void foo(Dog *&someDog) {
    someDog = new Dog("Fifi");
}
static void Main() 
{
    Dog myDog = new Dog("Rover");
    foo(ref myDog);
    // here, "myDog" points to the second Dog object, and has the name "Fifi"
    // the first Dog object still has the name "Rover"
}
static void foo(ref Dog someDog)
{
    someDog = new Dog("Fifi");
}