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! =-----
Jul 22 '05
16 1649
"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*****@hotmai l.com> wrote in message
news:c7******** @dispatch.conce ntric.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.n et... "Michael G" <mi****@montana .com> wrote in message news:40******** **@corp.newsgro ups.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.goo gle.com 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) {
document.images.src = eval("mt" +menu+ ".src")
}
alert("imgOff_hidemenu");
hideMenu=setTimeout('Hide(menu,num)',500);
|
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 code...
TCHAR myArray;
DoStuff(myArray);
|
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 construction and then..
// some other processing and/or changing 'retval'
return retval;
}
|
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 commands on the same connection.
I have an understanding that if SqlConnection is passed as "value"
(unboxed), object B will create its own copy of SqlConnection, so when
object A closes its connection, it remains open for object B's copy. Is this
|
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 original value of the local."
My code is:
using System;
using System.Collections.Generic;
| |
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.
function foo2(){}
function foo(func){}
|
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 value temporaries are
created?(I'm not touching yet the cases where standard allows
optimizations from the side of implementations to avoid copying)
If so, please quote part of the standard that says that.
Assuming it is true, I can imagine two...
|
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 Foo foo or readonlyFoo f?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |