By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

const char * and STL string reference

P: n/a
Hello all

In a few classes I have overloaded functions for C style strings and
STL strings like this

class SomeClass
{
void f(const char *s);
void f(const std::string &s);
};

At some point I forgot to include a C string function version in a
class, but noticed it all worked fine with const char *'s anyway...

In other words; const std::string &str = "blah"; works (a non const
reference won't). AFAIK this means it's a reference directly to a const
char *, can someone explain me why this would work?

Cheers, Rick

Sep 25 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Rick Helmus wrote:
In a few classes I have overloaded functions for C style strings and
STL strings like this

class SomeClass
{
void f(const char *s);
void f(const std::string &s);
};

At some point I forgot to include a C string function version in a
class, but noticed it all worked fine with const char *'s anyway...

In other words; const std::string &str = "blah"; works (a non const
reference won't). AFAIK this means it's a reference directly to a const
char *, can someone explain me why this would work?
First, a std::string has a constructor of the form string(const char*),
and that constructor is not declared explicit, so const char* (or const
char[]) is automagically converted to a std::string by the compiler
when it is expecting a std::string.

Second, a const reference can bind to a temporary, whose lifetime is
then extended to be the lifetime of the const reference. (A non-const
reference cannot bind to a temporary.)

So, what is going on here is the const char[] or const char* is
converted to a temporary std::string, and the const reference is bound
to that temporary.

That's all expected behavior.

Best regards,

Tom

Sep 25 '06 #2

P: n/a

Rick Helmus wrote:
Hello all

In a few classes I have overloaded functions for C style strings and
STL strings like this

class SomeClass
{
void f(const char *s);
void f(const std::string &s);
};

At some point I forgot to include a C string function version in a
class, but noticed it all worked fine with const char *'s anyway...

In other words; const std::string &str = "blah"; works (a non const
reference won't). AFAIK this means it's a reference directly to a const
char *,
No, it's a const reference to a std::string, just like it says it is.
The tricky bit is understanding *which* std::string it is a const
reference to, since at first glance there is noo std::string there.
can someone explain me why this would work?
#include <string>

class SomeClass
{
public:
// Oops! Forgot to implement void f(const char *s);
void f(const std::string &s) { /* do stuff ... */ }
};

int main()
{
SomeClass sc;
sc.f("blah");
}

When sc.f("blah") is called, an unnamed temporary std::string object is
created by the compiler and contructed using the std::string
constructor that takes a const char*, in this case using the value
"blah". The formal parameter s in the function is a const std::string&
which is bound to this unnamed temporary std::string. At the end of the
statement sc.f("blah"); the unnamed temporary std::string is destroyed.

const references may be bound to temporary objects. non-const
references may not, which is why you found that using a std::string&
rather than a const std::string& did not work.

Gavin Deane

Sep 25 '06 #3

P: n/a
Another mistery solved...
Thanks for the anwers guys :-)

Cheers, Rick

Sep 26 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.