C基准语义是不是在调用者的code明确 [英] c reference semantic isn't explicit in caller's code
问题描述
有人能解释一下吗?
老的C程序员有时并不喜欢引用因为它们提供引用语义,是不是在调用者的code明确。
块引用>解决方案当您使用C ++函数如下:
无效make42(INT&安培; X){X = 42; }
,因此称之为:
INT Y = 7;
make42(Y); //Ÿ现在是42再有就是的在
make42(Y)没有
的指示调用是
可以改变的。完全一样的函数调用将作出是否的不是的一通-by-reference参数:无效trymake42(INT X){X = 42; }INT Y = 7;
trymake42(Y); // y为7仍在C code,它会是这样的:
无效make42为(int * PX){
* PX = 42;
}
INT Y = 7;
make42(安培; Y);这就很清楚,在调用函数时更改
是
是一个现实的可能性。那么,这些老线C codeRS会关心的是这样一行:
someFunction(X);
在那里他们就没有从理念的只是的这是否
X
将改变与否。我不得不说,这些旧线codeRS之一,它远不如一个问题比报价似乎暗示。在没有看到样机,我甚至不能告诉的参数类型应该是什么,或者有多少,或它们的顺序。
所以,不得不参考原型说要弄清楚,如果参数是通过按值传递或按引用是不是一个真正的大问题。
C#需要用
退出
和REF
参数修饰符了不同的策略,但仍然使得它在呼叫明确该变量是通过按值传递或按引用。Can anybody explain this?
Old line C programmers sometimes don't like references since they provide reference semantics that isn't explicit in the caller's code.
解决方案When you use a C++ function like:
void make42 (int &x) { x = 42; }
and call it thus:
int y = 7; make42 (y); // y is now 42
then there is no indication in the
make42 (y)
call thaty
can be changed. Exactly the same function call would be made if it wasn't a pass-by-reference parameter:void trymake42 (int x) { x = 42; } int y = 7; trymake42 (y); // y is still 7
In C code, it would be something like:
void make42 (int *px) { *px = 42; } int y = 7; make42 (&y);
making it clear that a change to
y
is a real possibility when calling the function.So what these "old line" C coders would be concerned about is a line like:
someFunction (x);
where they would have no idea from just that whether
x
would change or not.I have to say that, as one of these old line coders, it's far less of a problem than the quote seems to imply. Without seeing the prototype, I can't even tell what the parameter types should be, or how many there are, or their order.
So, having to refer to said prototype to figure out if a parameter is pass-by-value or pass-by-reference is not really an big deal.
C# takes a different tack with the
out
andref
parameter modifiers but that still makes it explicit in the call whether the variable is pass-by-value or pass-by-reference.这篇关于C基准语义是不是在调用者的code明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!