g********l 发帖数: 68 | |
o**********t 发帖数: 406 | 2 you cannot allocate a new block of RAM in sub function if you pass pointer. |
s*****r 发帖数: 773 | 3 如果明确知道可以不为空,就用pass by refernece, 这样就不用每次都check 指针置
否为空了
void f( int* i) {
if(i == NULL) {
}
}
void f(int&i) {
//use i directly here.
}
【在 g********l 的大作中提到】 : 谢谢
|
w****m 发帖数: 146 | 4 second this
【在 s*****r 的大作中提到】 : 如果明确知道可以不为空,就用pass by refernece, 这样就不用每次都check 指针置 : 否为空了 : void f( int* i) { : if(i == NULL) { : } : } : void f(int&i) { : //use i directly here. : }
|
s*****r 发帖数: 773 | 5 啥意思?
【在 w****m 的大作中提到】 : second this
|
f*******e 发帖数: 1161 | 6 second=support
【在 s*****r 的大作中提到】 : 啥意思?
|
l******c 发帖数: 2555 | 7 in theory, correct; in practice, reference parameter will crash the system
【在 s*****r 的大作中提到】 : 如果明确知道可以不为空,就用pass by refernece, 这样就不用每次都check 指针置 : 否为空了 : void f( int* i) { : if(i == NULL) { : } : } : void f(int&i) { : //use i directly here. : }
|
s*****r 发帖数: 773 | 8 can you elaborate it?
【在 l******c 的大作中提到】 : in theory, correct; in practice, reference parameter will crash the system
|
f****4 发帖数: 1359 | 9 能给个例子不?
【在 l******c 的大作中提到】 : in theory, correct; in practice, reference parameter will crash the system
|
l******c 发帖数: 2555 | 10 int * p = 0;
int & rp = *p;
void f(int & ri)
{
ri = 1; //crash
};
void main()
{
f(rp);
};
【在 f****4 的大作中提到】 : 能给个例子不?
|
f*******e 发帖数: 1161 | 11 我觉得这个问题还是出在解饮用上,就是说*p之前要保证p是有效的
与本身引用传递并无太大关系。
【在 l******c 的大作中提到】 : int * p = 0; : int & rp = *p; : void f(int & ri) : { : ri = 1; //crash : }; : void main() : { : f(rp); : };
|
f*****r 发帖数: 229 | 12 f(const type& arg) is preferrable by some people, since it has no risk, and
it is more readable compared with f(const type* arg).
【在 g********l 的大作中提到】 : 谢谢
|