在C ++中对给定函数进行完整性检查 [英] Sanity check of the given function in C++

查看:83
本文介绍了在C ++中对给定函数进行完整性检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在接受采访时被问到以下问题,并被问及是否有健全检查问题。任何人都可以帮助我:



//以下函数应该增加x的值

Hi All,
I was asked following question in an interview and was asked that it is having sanity check issues. Could anyone please help me in this:

//following function should increment the value of x

void increment(int x)
{
    x++;
    return; 
}





问候



我是什么尝试过:



我已经要求按值更改副本以通过引用传递它仍然给出错误。



Regards

What I have tried:

I have asked to change copy by value to pass by reference by it was still giving error.

推荐答案

此代码不应该给出错误。如果通过引用传递,则x将在调用代码中递增。如果你更改了它并得到了错误,请给我们新的代码和错误如果你被卡住了
This code should not give an error. If you pass by reference, then x will be incremented in the calling code. If you changed it and got an error, give us the new code and the error if you're stuck


这个函数中的一个问题可能是它有没有外部效应。输入x在堆栈上创建,在本地修改并被销毁。



因此该函数仅修改本地x,仅此而已。您可以使用& -operator更改此行为,如前所述。
One issue in this function can be that it has NO EXTERNAL EFFECTS. The input x is created on the stack, locally modified and than destroyed.

So the function only modies the local x and nothing more. You can change this behaviour with the &-operator, as mentioned before.


这篇关于在C ++中对给定函数进行完整性检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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