Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Convert blank string to reference

Posted on 2004-09-29
2
Medium Priority
?
210 Views
Last Modified: 2010-04-01
I have a function which returns a reference to a string(std::string).  How do I go about returning a reference to a blank string if certain conditions are not met?
0
Comment
Question by:jewee
2 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12181977
I guess you name "reference" to a pointer? That is:

std::string *SomeFunction(arguments here)

You can always return a NULL pointer if function fails, just

(inside function)
return NULL;

(outside function)
std::string *aString=SomeFunction(arguments here)

if (aString!=NULL) {
     // some action here
}
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 12182024
>>How do I go about returning a reference to a blank string if certain conditions are not met?

A reference to a blank string is just the same as a reference to an empty string, e.g.

void GetQueryResults ( string& strQuery, string& strResults) {

    strResults = ""; // blank out string first, so we return an empty result set if nothing found

     // add results to strResults, if any

    // if nothing was would, strResult is set to empty/blank, i.e. strResults.size() == 0
    // and strResults.empty() == true
}
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question