通过引用传递参数 [英] passing an argument by reference
问题描述
我有一个api,它使用引用作为参数。
void函数(double z,short& x,short& y){}
一些计算是在z上完成的,然后结果被传回
输出x和y。
我需要传递给它的值函数是指向短裤的指针。
短* x1;
短* y1;
如果我打电话给功能类似
函数(z1,* x1,* y1);
编译器没有抱怨我得到了正确的结果我在其上执行的测试数量很少。但我不确定这是否正确。
谢谢,迈克
----- =通过Newsfeeds.Com发布,未经审查的Usenet新闻= -----
http:// www.newsfeeds.com - 世界排名第一的新闻组服务!
----- ==超过100,000个新闻组--19个不同的服务器! = -----
On Sun,2004年5月2日22:50:03 -0600,Michael G < mi **** @ montana.com>
在comp.lang.c ++中写道:
我有一个使用引用作为参数的api。
void函数(double z,short& x,short& y){}
有些计算是在z上完成的,然后结果将被传回
x和y。
我需要传递给这个函数的值是指向short的指针。
short * x1;
short * y1;
如果我将函数称为
函数(z1,* x1,* y1);
编译器没有抱怨我得到了正确的结果我对它进行了少量的测试。但我不确定这是否正确。
谢谢,Mike
假设x1和y1已正确初始化并且实际上指向
短整数,这很好。
传递的内容是对指针指向的短路的引用
到。
-
Jack Klein
主页: http://JK-Technology.Com
常见问题解答
comp.lang.c < a rel =nofollowhref =http://www.eskimo.com/~scs/C-faq/top.htmltarget =_ blank> http://www.eskimo.com/~scs/C -faq / top.html
comp.lang.c ++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c ++
< a rel =nofollowhref =http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html\"target =_ blank> http://www.contrib.andrew。 cmu.edu/~a...FAQ-acllc.html
" Michael G" < MI **** @ montana.com>在消息中写道
news:40 ********** @ corp.newsgroups.com ...我有一个使用引用作为参数的api 。
void函数(double z,short& x,short& y){}
一些计算在z上完成,然后结果传递
退出在x和y中。
我需要传递给这个函数的值是指向short的指针。
short * x1;
short * y1;
如果我把函数称为
函数(z1,* x1,* y1);
编译器没有抱怨我得到了正确的我在其上执行的测试数量很少。但我不确定
这是否正确。
谢谢,Mike
函数调用正确,前提是x1和y1实际上指的是
短裤,即
短a;
短b;
short * x1 =& a;
short * x2 =& b;
Michael G写道:
< blockquote class =post_quotes>我有一个使用引用作为参数的api。
void函数(double z,short& x,short& y);
这是一个非常糟糕的主意。
一些计算在z
上进行,然后结果将在x和y中传回。
* Real * C ++程序员这样做:
cat main.cc
#include< iostream>
std :: pair< short,short> f(double z);
int main(int argc,char * argv []){
std :: pair< short,short> p = f(33.0);
std :: cout<< p.first<< , << p.second<< std :: endl;
返回0;
}
I have an api that uses reference as arguments.
void function(double z, short &x, short &y){}
some calculations are done on z and then the results are being passed back
out in x and y.
The values the i need to pass to this function are pointer to shorts.
short * x1;
short * y1;
If I call the function like
function(z1, *x1, *y1);
The compiler doesn''t complain and I get correct results with the small
number of tests that I have performed on it. But I am not sure that this is
correct.
Thanks, Mike
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
On Sun, 2 May 2004 22:50:03 -0600, "Michael G" <mi****@montana.com>
wrote in comp.lang.c++:
I have an api that uses reference as arguments.
void function(double z, short &x, short &y){}
some calculations are done on z and then the results are being passed back
out in x and y.
The values the i need to pass to this function are pointer to shorts.
short * x1;
short * y1;
If I call the function like
function(z1, *x1, *y1);
The compiler doesn''t complain and I get correct results with the small
number of tests that I have performed on it. But I am not sure that this is
correct.
Thanks, Mike
Assuming that x1 and y1 are properly initialized and actually point to
short ints, this is just fine.
What are passed are references to the shorts that the pointers point
to.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
"Michael G" <mi****@montana.com> wrote in message
news:40**********@corp.newsgroups.com...I have an api that uses reference as arguments.
void function(double z, short &x, short &y){}
some calculations are done on z and then the results are being passed back out in x and y.
The values the i need to pass to this function are pointer to shorts.
short * x1;
short * y1;
If I call the function like
function(z1, *x1, *y1);
The compiler doesn''t complain and I get correct results with the small
number of tests that I have performed on it. But I am not sure that this is correct.
Thanks, Mike
The function call is correct provided x1 and y1 actually point to
shorts, i.e.
short a;
short b;
short * x1 = &a;
short * x2 = &b;
Michael G wrote:
I have an api that uses reference as arguments.
void function(double z, short &x, short &y);
This is a very bad idea.
some calculations are done on z
and then the results are being passed back out in x and y.
*Real* C++ programmers do it like this:
cat main.cc
#include <iostream>
std::pair<short, short> f(double z);
int main(int argc, char* argv[]) {
std::pair<short, short> p = f(33.0);
std::cout << p.first << ", " << p.second << std::endl;
return 0;
}
这篇关于通过引用传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!