Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > .. a way to return two references from a function ?

Reply
Thread Tools

.. a way to return two references from a function ?

 
 
Matthias Buelow
Guest
Posts: n/a
 
      02-27-2008
Puppet_Sock wrote:

>> i try to write a class-function which can return two arguments by
>> reference.

> Don't. Re-factor your problem so that does not happen.
> Socks


I disagree, returning a small number of values (2, 3) in a struct is in
many cases an elegant solution and doesn't necessarily incur a
performance hit.

Quite a few times I have wished for C or C++ to have multiple return
values; if you've worked with them, you tend to miss them in languages
that don't have them.
 
Reply With Quote
 
 
 
 
Joe Greer
Guest
Posts: n/a
 
      02-27-2008
Matthias Buelow <> wrote in news:62lo6lF23mqf3U1
@mid.dfncis.de:

> Puppet_Sock wrote:
>
>>> i try to write a class-function which can return two arguments by
>>> reference.

>> Don't. Re-factor your problem so that does not happen.
>> Socks

>
> I disagree, returning a small number of values (2, 3) in a struct is in
> many cases an elegant solution and doesn't necessarily incur a
> performance hit.
>
> Quite a few times I have wished for C or C++ to have multiple return
> values; if you've worked with them, you tend to miss them in languages
> that don't have them.
>


The OP might want to look at tr1::tuple() and tr1::tie(), They provide the
ability to return and assign multiple values at once. If tr1 isn't
available, then look at the boost libraries. They have implementations of
these.

joe
 
Reply With Quote
 
 
 
 
Tomás Ó hÉilidhe
Guest
Posts: n/a
 
      02-27-2008

> I've tried the following:
>
> DataUnit returnValue = { m_someobject1,m_someobject2};
>
> but it doesnt work.



The following compiles just fine for me:

struct TwoIntRefs {
int &i, &j;
};

TwoIntRefs Func()
{
static int a = 55, b = 44;

TwoIntRefs const tir = {a,b};

return tir;
}

int main()
{
TwoIntRefs const tir = Func();

tir.i = 77;

tir.j = 33;
}
 
Reply With Quote
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      02-27-2008
"Tomás Ó hÉilidhe" <> wrote in message
news:a73d7968-1705-4573-a23d-
...
> The following compiles just fine for me:
>
> struct TwoIntRefs {
> int &i, &j;
> };


There is also std:air<int&,int&>, although then you have to refer to
things as "first" and "second".
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      02-27-2008
Lutz Altmann wrote:
>
> i try to write a class-function which can return two arguments by
> reference.
> My idea was to encapsulate the references in a struct :
>
> struct DataUnit
> {
> SomeClassType& m_object1;
> SomeOtherClassType& m_object2;
> };
>
> So that the function can return the two references by using the
> struct :
>
> DataUnit processSomething();
>
> My question is: How can i create the struct in the function ?
> I've tried the following:
>
> DataUnit returnValue = { m_someobject1,m_someobject2};
>
> but it doesnt work.
> ...


What exactly "doesn't work"?

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
Lutz Altmann
Guest
Posts: n/a
 
      02-28-2008
On 27 Feb., 18:17, "Eric.Malenf...@gmail.com"
<Eric.Malenf...@gmail.com> wrote:
> Lutz Altmann wrote:
> > hello,

>
> > i try to write a class-function which can return two arguments by
> > reference.
> > My idea was to encapsulate the references in a struct :

>
> > struct DataUnit
> > {
> > SomeClassType& m_object1;
> > SomeOtherClassType& m_object2;
> > };

>
> [snip]
>
> Boost.Tuple seems to fit nicely here :http://www.boost.org/libs/tuple/doc/...tml#constructi...
> Look at the make_tuple section and, in particular, to the use of ref()
> and cref().
>
> Non-tested example:
> class SomeClass
> {
> public:
> boost::tuple<SomeClassType&, SomeOtherClassType&> Foo()
> {
> return boost::make_tuple(boost::ref(m_SomeClass),
> boost::ref(m_SomeOtherClass));
> }
> private:
> SomeClassType m_SomeClass;
> SomeOtherClassType m_SomeOtherClass;
> };
>
> HTH,
>
> Éric Malenfant


nice,

for me it seems to be a very elegant solution.
i think i'll use this one or the template provided by the stl
std:air<int&,int&>

thanks a lot
 
Reply With Quote
 
Lutz Altmann
Guest
Posts: n/a
 
      02-28-2008
On 27 Feb., 22:14, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
> Lutz Altmann wrote:
>
> > i try to write a class-function which can return two arguments by
> > reference.
> > My idea was to encapsulate the references in a struct :

>
> > struct DataUnit
> > {
> > SomeClassType& m_object1;
> > SomeOtherClassType& m_object2;
> > };

>
> > So that the function can return the two references by using the
> > struct :

>
> > DataUnit processSomething();

>
> > My question is: How can i create the struct in the function ?
> > I've tried the following:

>
> > DataUnit returnValue = { m_someobject1,m_someobject2};

>
> > but it doesnt work.
> > ...

>
> What exactly "doesn't work"?
>
> --
> Best regards,
> Andrey Tarasevich


sorry it was my fault - what i described at the beginning was not
really wrong ..
there was actually no problem with the code - blame on me
 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      02-28-2008
In message <>, Matthias Buelow
<> writes
>Puppet_Sock wrote:
>
>>> i try to write a class-function which can return two arguments by
>>> reference.

>> Don't. Re-factor your problem so that does not happen.
>> Socks

>
>I disagree, returning a small number of values (2, 3) in a struct is in
>many cases an elegant solution and doesn't necessarily incur a
>performance hit.


I think the objection is to returning references, not tuples.

>
>Quite a few times I have wished for C or C++ to have multiple return
>values; if you've worked with them, you tend to miss them in languages
>that don't have them.


--
Richard Herring
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function Variable and Return Value References Eric Hofreiter Ruby 4 11-12-2005 02:58 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
how to understand references to variables and references to constants are distinguished? baumann.Pan@gmail.com C++ 3 11-10-2004 04:16 AM
Pointers and References (and References to Pointers) Roger Leigh C++ 8 11-17-2003 10:14 AM



Advertisments