const char * and STL string reference

R

Rick Helmus

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
 
T

Thomas Tutone

Rick said:
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
 
G

Gavin Deane

Rick said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top