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! =----- 16 1617
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;
} std::pair<short, short> f(double z);
Why should we do simple when we can do complicated ?
"Boris Sargos" <bs*****@wanadoo.fr> wrote in message
news:c7**********@news-reader2.wanadoo.fr... std::pair<short, short> f(double z);
Why should we do simple when we can do complicated ?
Which one do you consider to be more complicated?
john
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message news:<40**************@jpl.nasa.gov>... 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; }
Well - those two functions are different. Your function has no access
to the initial values of x and y.
/Peter
"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.
Well, not exactly. You need to *get* the values from pointers 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.
It is. It might help to look at it like this.
short x2;
short y2;
x2 = *x1;
y2 = *y1;
function(z1, x2, y2);
Make more sense now?
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:40**************@jpl.nasa.gov... 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:
Oh boy. That's just what the OP needs......
Peter Koch Larsen wrote: E. Robert Tisdale wrote:
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; }
Well - those two functions are different. Your function has no access to the initial values of x and y.
Mike's original description of the API
describes x and y as outputs -- not inputs.
But here is a version which passes them as inputs:
cat main.cc
#include <iostream>
std::pair<short, short> f(short x, short y, double z);
int main(int argc, char* argv[]) {
short x = 7, y = 13;
std::pair<short, short> p = f(x, y, 33.0);
x = p.first;
y = p.second;
std::cout << x << ", " << y << std::endl;
return 0;
}
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:40**************@jpl.nasa.gov... Mike's original description of the API describes x and y as outputs -- not inputs. But here is a version which passes them as inputs:
> cat main.cc #include <iostream>
std::pair<short, short> f(short x, short y, double z);
int main(int argc, char* argv[]) {
You don't need argc and argv here.
short x = 7, y = 13; std::pair<short, short> p = f(x, y, 33.0); x = p.first; y = p.second; std::cout << x << ", " << y << std::endl; return 0; }
Mine is better.
#include <iostream>
template<class A, class B, class C>
std::pair<A, B> f(A x, B y, C z);
int main()
{
short x = 7, y = 13;
std::pair<short, short> p = f(x, y, 33.0);
x = p.first;
y = p.second;
std::cout << x << ", " << y << std::endl;
}
And happiness never ends.
Ioannis Vranos
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:40**************@jpl.nasa.gov... 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.
I'm guessing from the original post, that this is not something he gets to
choose. He says "I have an api that..", from which I gather that he's using
an existing api, not writing one.
Besides, his question is not about the design of the call, but how to use it
properly. 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; }
Wow. And here I thought I *was* a *real* C++ programmer. I guess I'm
imaginary. (So please feel free to ignore me. :-)) I doubt that I'd
create a whole new type simply for the purpose of getting a pair of values
out of a function. If my program was using such a type already, sure...that
would work great. Otherwise, it's kind of overkill.
But, what I might question is whether the OP really needs pointers to shorts
in the first place. If he's doing that because of the signature of this
function, that's not what's needed. Simply passing the variables themselves
is appropriate. But if he has pointers to shorts anyway, and needs to pass
their values to this function, then he's doing it correctly.
-Howard
Howard wrote: Wow. And here I thought I *was* a *real* C++ programmer. I guess I'm imaginary. (So please feel free to ignore me. :-)) I doubt that I'd create a whole new type simply for the purpose of getting a pair of values out of a function.
No. But I would ask myself,
"Why would I get two unrelated values back from a function call?"
"Aren't they really related parts of a single object?"
And then I would define a class to represent that object. If my program was using such a type already, sure...that would work great. Otherwise, it's kind of overkill.
But, what I might question is whether the OP really needs pointers to shorts in the first place.
He does not.
If he's doing that because of the signature of this function, that's not what's needed. Simply passing the variables themselves is appropriate. But if he has pointers to shorts anyway, and needs to pass their values to this function, then he's doing it correctly.
I'm sure that everyone agrees.
But the API is the root of the problem.
We can't tell from the signature alone:
void f(double z, short& x, short& y);
whether x and y are inputs that need to be initialized
before calling this function or whether they are simply outputs
which can be passed to f(z, x, y) uninitialized.
A *real* C++ programmer would avoid this ambiguity by
std::pair<short, short> f(short x, short y, double z);
returning the pair by value instead.
"Howard" <al*****@hotmail.com> wrote in message
news:c7********@dispatch.concentric.net...
But, what I might question is whether the OP really needs pointers to
shorts in the first place. If he's doing that because of the signature of this function, that's not what's needed. Simply passing the variables
themselves is appropriate. But if he has pointers to shorts anyway, and needs to
pass their values to this function, then he's doing it correctly.
Yeah. I was given an api with references as parameters and I was also
suppose to place this new api function into some existing code. My superior
frowns upon additional declaration, etc. so I was just trying to make due
with what I was handed. And of course I want it to be correct.
Thanks to everyone for the disscussion.
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! =-----
"jeffc" <no****@nowhere.com> wrote in message
news:40********@news1.prserv.net... "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.
Well, not exactly. You need to *get* the values from pointers 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.
It is. It might help to look at it like this.
short x2; short y2; x2 = *x1; y2 = *y1; function(z1, x2, y2); Make more sense now?
Yes it does.
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! =-----
"Ioannis Vranos" <iv*@guesswh.at.grad.com> wrote: #include <iostream>
template<class A, class B, class C> std::pair<A, B> f(A x, B y, C z);
int main() { short x = 7, y = 13; std::pair<short, short> p = f(x, y, 33.0);
It would be nice if the language didn't require you to repeat
the type of p. It is obviously deducible from f, something like:
_Let p = f(x, y, 33.0);
Has this ever been proposed?
Old Wolf wrote in news:84**************************@posting.google.c om in
comp.lang.c++: "Ioannis Vranos" <iv*@guesswh.at.grad.com> wrote: #include <iostream>
template<class A, class B, class C> std::pair<A, B> f(A x, B y, C z);
int main() { short x = 7, y = 13; std::pair<short, short> p = f(x, y, 33.0);
It would be nice if the language didn't require you to repeat the type of p. It is obviously deducible from f, something like: _Let p = f(x, y, 33.0); Has this ever been proposed? http://std.dkuug.dk/jtc1/sc22/wg21/d...2004/n1607.pdf
The proposed syntax would be:
auto p = f( x, y, 33.0 );
Rob.
-- http://www.victim-prime.dsl.pipex.com/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: domeceo |
last post by:
can anyone tell me why I cannot pass values in a setTimeout function
whenever I use this function it says "menu is undefined" after th
alert.
function imgOff(menu, num) {
if (document.images) {...
|
by: jr |
last post by:
Sorry for this very dumb question, but I've clearly got a long way to go!
Can someone please help me pass an array into a function. Here's a starting
point.
void TheMainFunc()
{
// Body of...
|
by: Victor Bazarov |
last post by:
In the project I'm maintaining I've seen two distinct techniques used for
returning an object from a function. One is
AType function(AType const& arg)
{
AType retval(arg); // or default...
|
by: LP |
last post by:
Hello,
Here's the scenario:
Object A opens a Sql Db connection to execute number of SqlCommands. Then it
needs to pass this connection to a constructor of object B which in turn
executes more...
|
by: MSDNAndi |
last post by:
Hi,
I get the following warning:
"Possibly incorrect assignment to local 'oLockObject' which is the argument
to a using or lock statement. The Dispose call or unlocking will happen on
the...
|
by: Andrew Bullock |
last post by:
Hi,
I have two classes, A and B,
B takes an A as an argument in its constructor:
A a1 = new A();
B b = new B(a1);
|
by: Mike |
last post by:
Consider the following code:
"""
struct person {
char *name;
int age;
};
typedef struct person* StructType;
|
by: User1014 |
last post by:
Since you can pass a function to a ... erm...... function....
how to you use the result of a function as the argument for another
function instead of passing the actual function to it.
i.e.
...
|
by: dave_dp |
last post by:
Hi, I have just started learning C++ language..
I've read much even tried to understand the way standard says but still
can't get the grasp of that concept.
When parameters are passed/returned...
|
by: puzzlecracker |
last post by:
How can I pass a reference to a method as constant?
I tried the following:
Function(const Foo f) or Function(readonly Foo f)
Also, How to declare local variable to be constant
const...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |