C ++参数传递查询(包括代码示例和输出) [英] C++ parameter passing queries (code examples and outputs included)

查看:103
本文介绍了C ++参数传递查询(包括代码示例和输出)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我再次在这里是因为C ++的课程资料完全没有很好地讲授.这个问题给出了使用几个函数的定义,并调用了这些函数,并期望我们声明输出以及到底发生了什么.我已经执行了这些功能,并试图对正在发生的事情提出一些理由,但是如果有人可以帮助我,我将非常感激.

Again here i am because the course material for C++ was not taught very well at all. The question gives use several function definitions and calls to these functions and expects us to state the output and what exactly is going on. I have executed these functions and tried to come up with some rationale to what is happening but if someone could help me out that i would be very grateful.

函数定义(名字都是给定的.我知道名字不是很好):

function definitions (The names are all as given. I know the names are not very good):

void MyIncrementFirst(int * i) {
(*i)++;
}

void MyIncrementSecond(int i) {
i++;
}

void MyIncrementThird(int & i) {
i++;
}
void MyIncrementFourth(int ** i) {
*i = new int(0);
}

void MyIncrementFifth(int *& i) {
i = new int(69);
}

对这些函数的调用已定义为:

The calls to these functions have been defined as:

int * a = new int(42);
cout << "Result" << endl;

MyIncrementFirst(a);
cout << "first " << *a << endl;

MyIncrementSecond(*a);
cout << "second " <<*a << endl;

MyIncrementThird(*a);
cout << "third " <<*a << endl;

MyIncrementFourth(&a);
cout << "fourth " <<*a << endl;

MyIncrementFifth(a);
cout << "fifth " <<*a << endl;

会产生以下内容:

first 43
second 43
third 44
fourth 0
fifth 69 

我以前从未见过这种语法(例如*&),所以我在这里可能完全错了.

Some of this syntax i have not seen before (e.g. *&) so i maybe completely wrong here.

第一:

这似乎很简单.我传入一个指向整数的指针,然后取消对该指针的引用并增加其值,所以我没有增加指针,而是增加了它所指向的实际值.不使用任何内存分配,并且此函数使用按引用传递"来修改该值.

This seems simple enough. I am passing in a pointer to an integer, and then de-referencing the pointer and incrementing its value, so instead of incrementing the pointer i am incrementing the actual value it is pointing to. No memory allocation is used, and this function uses pass-by-reference in order to modify the value.

第二:

这与上面的类似,因为取消了指针的引用以从指针而不是内存地址获取整数值.现在,此函数使用值传递,因此在函数执行中使用了变量的副本.这意味着在此处增加a不会对您传入的变量产生任何影响,a的值将保持为42,但是在函数执行过程中,传入的变量的副本的值将变为43.

This is similar to the above, as the pointer is de-referenced to get the integer value from the pointer rather than the memory address. Now this function uses pass-by-value so a copy of the variable is used in the functions execution. This means that incrementing a here, will have no impact on the variable you passed in, the value of a will remain as 42, but the copy of the passed in variable will have its value changed to 43 during the functions execution.

第三:

我的理解是'&'运算符基本上是在说"...的内存地址",因此该函数将采用传入指针的内存地址,将其指向的值定位为递增1.这就是为什么打印43的原因.这是通过引用传递.

My understanding is that the '&' operator is basically saying 'the memory address of ...' so this function will take the memory address of the passed in pointer, locate the value it points to an increment this by one. That is why 43 is printed. This is using pass-by-reference.

第四:

如果我没记错的话,'**'语法表示指向指针的指针.这意味着在将& a传递给函数时,您正在传递指针的内存地址,因此,当取消引用该指针并将其值设置为new int(0);时,实际上将覆盖该地址处的现有数据(42 (在这种情况下),因此将打印值0.这使用了按引用传递.

If i remember correctly the '**' syntax means a pointer to a pointer. This means that when passing &a to the function you are passing the memory address of the pointer so when you de-reference this pointer and set its value to a new int(0); you are actually overwriting the exiting data at this address (42 in this case) so the value 0 is printed. This uses pass-by-reference.

第五:

(*&)之前我还没有看到这种语法.我已经查看了这种语法,并且我认为它基本上是在说通过引用传递我的值而不是值",因此它似乎与前面提到的取消引用指针相同.再次,因为当值设置为new int(69);时,这不是通过引用的值,所以该位置的当前数据将被覆盖,因此输出69.

I have not seen this syntax before (*&). I have looked up this syntax and i think it is basically saying 'pass me by reference not value' so it seems to be the same as de-referencing the pointer as mentioned earlier. Again, because this is pass-by-refence not value when the value is set to a new int(69); the current data at that location is overwritten thus 69 is outputted.

忘记包括我的问题!我想知道我的想法是否正确,这就是所有考试准备,所以我想确保自己做得正确.我只想确保自己的逻辑正确,并且如果我做错了什么或错过了什么,就会有人能够指引我正确的方向

Forgot to include my question! I want to know if my thinking is correct, this is all exam prep so i want to be sure i am doing it correctly.I just want to make sure my logic is correct and if i have done anything wrong or missed anything would somebody be able to steer my in the right direction

推荐答案

(基于一些评论者的反馈)

(based on some commenter feedback)

您所拥有的一切基本上都是正确的,请避免混淆您的输出值.但是,您在#4和#5中拥有的实际上不是覆盖数据,它实际上是位于新地址的新数据,该地址已分配给变量i.这是较早的评论者所说的潜在内存泄漏问题的一部分.由于您没有在分配新地址之前删除旧地址,因此可能会出现新地址.

Everything you have is mostly correct, save some confusion on your output values. However, what you have in #4 and #5 is actually not overwritten data, it's actually new data at a new address, which is assigned to the variable i. This is part of what an earlier commenter said about a potential for a memory leak. Since you're not deleting the old address before assigning a new one, that's where it could occur.

最后一个中的&*语法是对指针的引用.它与int**基本上相同,只是您不传递&a作为参数,只是a

The &* syntax in the last one is a reference to a pointer. It's essentially the same as int** except you don't pass &a as the argument, it's just a

数字5的内容基本上是正确的,但其背后的原因却缺少&*

What you have there for number 5 is basically correct, but the reasoning behind it is missing the meaning of &*

您可能想弄清楚是依次还是一次运行了每个函数调用,因为如果您确实按书面顺序依次运行它们,则输出是不正确的.

You might want to clarify if you've run each of these function calls sequentially or one at a time, since your output is incorrect if you'd indeed run them sequentially as written.

这篇关于C ++参数传递查询(包括代码示例和输出)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆