473,396 Members | 1,907 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,396 software developers and data experts.

return a string by ref

cpp
I have the following code

class A
{
private:
string s1;
string s2;
string s3;
string s4;

// Need a function f1 which returns one of the 4 strings based on

public:
A()
{
// initialize s1, s2, s3, s4
}

void f2()
{
// will use s1, s2, s3 based on the value returned by f1
// will not modify the string
}

};

What should be the signature of f1?

do any of the following look like the right thing to use
string & f1() ;
const string & f1();


Jul 27 '06 #1
3 1308
Hello

Your question is not clear.

It is better to create the strings as an array as:

string s[4]

Then f1() returns a const reference to any of s[0] to s[3] depending on some
condition

Say,
const string &f1(int idx)
{
// idx bounds checks here
return s[idx];
}

HTH
Elias
"cpp" <cp*@mailinator.comwrote in message
news:4i************@individual.net...
>I have the following code

class A
{
private:
string s1;
string s2;
string s3;
string s4;

// Need a function f1 which returns one of the 4 strings based on

public:
A()
{
// initialize s1, s2, s3, s4
}

void f2()
{
// will use s1, s2, s3 based on the value returned by f1
// will not modify the string
}

};

What should be the signature of f1?

do any of the following look like the right thing to use
string & f1() ;
const string & f1();


Jul 27 '06 #2
cpp wrote:
I have the following code

class A
{
private:
string s1;
string s2;
string s3;
string s4;

// Need a function f1 which returns one of the 4 strings based on
Based on what?
public:
A()
{
// initialize s1, s2, s3, s4
}

void f2()
{
// will use s1, s2, s3 based on the value returned by f1
// will not modify the string
}

};

What should be the signature of f1?

do any of the following look like the right thing to use
string & f1() ;
const string & f1();
It depends on what you need. If you want 'f1' to explicitly restrict the
calling code from modifying the strings, then you use the second
variant. Or, even better, 'const string & f1() const'. This will also be
callable from 'const' member functions. The first variant is not.

--
Best regards,
Andrey Tarasevich

Jul 27 '06 #3
cpp posted:
class A
{
private:
string s1;
string s2;
string s3;
string s4;

// Need a function f1 which returns one of the 4 strings based on

string const &f1(unsigned const i) const
{
assert(i <= 4);

switch (i)
{
case 1: return s1;
case 2: return s2;
case 3: return s3;
case 4: return s4;
}
}

string &f1(unsigned const i)
{
return const_cast<string&>( static_cast<A const*>(this)->f1(i) );
}

--

Frederick Gotham
Jul 27 '06 #4

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

Similar topics

3
by: Phil Powell | last post by:
My first time working with a PHP class, and after 6 hours of working out the kinks I am unable to return a value from the class, so now I appeal to the general audience what on earth did I do wrong...
12
by: Jose Fernandez | last post by:
Hello. I'm building a web service and I get this error. NEWS.News.CoverNews(string)': not all code paths return a value This is the WebMethod public SqlDataReader CoverNews(string Sport)...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
7
by: nafri | last post by:
hello all, I want to create a function that returns the first element of the Array that is input to it. However, the Input Array can be an Array of points, double, or anyother type, which means...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
9
by: MSDNAndi | last post by:
Hi, I have a set of simple webservices calls that worked fine using .NET Framework 1.0. I am calling a Java/Apache based webservices, the calling side is not able to supply a proper WSDL. ...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
2
by: utab | last post by:
Dear all, I tried sth easy(actually this was an exercise) but I tried to use the standard lib. heavily for this problem(as far as I can). There was one point I could not figure out. The problem...
6
KoreyAusTex
by: KoreyAusTex | last post by:
If anyone can help me figure out the what the missing return statements are, I think it might be the fact that I need to add a return false in the getValue()? import java.util.*; public class...
14
by: =?Utf-8?B?QmVu?= | last post by:
Hi all, I'm trying to understand the concept of returning functions from the enclosing functions. This idea is new to me and I don't understand when and why I would need to use it. Can someone...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
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...
0
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...
0
agi2029
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,...

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.