473,320 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

ways to return multiple vlaues?

hey, which ways are available i c++ to return multiple values
i know u can use structs, any other methods?

concrete question: i want to do sth. like this:
returning two strings into stringvars data and type
<data,type>=<string,stringsplittedAttribs()
strucs would lead to big changes in source...
Dec 4 '06 #1
3 1879
"Oliver Bleckmann" <Ol**************@freenet.dewrites:
hey, which ways are available i c++ to return multiple values
i know u can use structs, any other methods?

concrete question: i want to do sth. like this:
returning two strings into stringvars data and type
<data,type>=<string,stringsplittedAttribs()
strucs would lead to big changes in source...
Arguments by reference spring to mind. You could use the argument list
of your function. Example:

int splittedAttribs(String& data_str, String& type_str)
{
int retval = 0; // may return error status or so...

data_str = "hello";
type_str = "world";

...

return retval;
}

Cheers,
Rudiger
Dec 4 '06 #2
Oliver Bleckmann wrote:
hey, which ways are available i c++ to return multiple values
i know u can use structs, any other methods?

concrete question: i want to do sth. like this:
returning two strings into stringvars data and type
<data,type>=<string,stringsplittedAttribs()
strucs would lead to big changes in source...
Use std::pair, or for more than two, use their extension:
std::tr1::tuple (aka boost::tuple). For an unspecified number of return
values, use a vector (or auto_ptr< vector<). If the types are
different, you could use std::vector<boost::anyor similar.

I'm not sure what you are trying to do in your "concrete" question, but
you can probably use ordinary assignment or std::tr1::tie().

You might also consider passing in references as was suggested
previously.

Cheers! --M

Dec 4 '06 #3
Oliver Bleckmann wrote:
hey, which ways are available i c++ to return multiple values
i know u can use structs, any other methods?

concrete question: i want to do sth. like this:
returning two strings into stringvars data and type
<data,type>=<string,stringsplittedAttribs()
strucs would lead to big changes in source...
I would recommend using tuples both for their convenience and clarity -
because unlike reference parameters, returning a tuple leaves no doubt
whether a particular value is an input or output parameter.

For example:

#include <iostream>
#include <string>
#include <tr1/tuple>

using std::tr1::tuple;
using std::tr1::make_tuple;
using std::tr1::tie;

tuple<int, char, std::stringf()
{
return make_tuple(5, 'a', "some text");
}

int main()
{
int i;
char c;
std::string s;

tie( i, c, s ) = f();

std::cout << "i: " << i << "\n";
std::cout << "c: " << c << "\n";
std::cout << "s: " << s << "\n";
}

Program Output:

i: 5
c: a
s: some text

Greg

Dec 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
94
by: John Bailo | last post by:
The c# *return* statement has been bothering me the past few months. I don't like the fact that you can have different code paths in a method and have multiple return statements. To me, it...
27
by: Maximus | last post by:
Hi, I was just wondering, is it good to use return without arguments in a void function as following: void SetMapLayer() { if( !Map ) return; layer = LAYER_MAP; }
24
by: sureshjayaram | last post by:
In some functions where i need to return multiple error codes at multiple places, I use multiple return statements. Say for ex. if (Found == 1) { if (val == -1) return error1; } else { if...
7
by: J-T | last post by:
I can instantiate my object in my *ASP.NET* application in two ways: A) public sealed class RSSingleton { private static ReportingServiceProxy m_RsProxy=null; static RSSingleton() {...
16
by: Nikolay Petrov | last post by:
How can I return multiple values from a custom function? TIA
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
8
by: aleksandar.ristovski | last post by:
Hello all, I have been thinking about a possible extension to C/C++ syntax. The current syntax allows declaring a function that returns a value: int foo(); however, if I were to return...
4
by: Jonathan | last post by:
I have a SQL stored procedure for adding a new record in a transactions table. It also has two return values: CounterID and IDKey. I want to create a webservice that accepts the 10 input...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.