Solved

Convert blank string to reference

Posted on 2004-09-29
2
202 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

932 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now