472,790 Members | 3,571 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,790 software developers and data experts.

const char * and STL string reference

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
3 4273
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

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
Another mistery solved...
Thanks for the anwers guys :-)

Cheers, Rick

Sep 26 '06 #4

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

Similar topics

3
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. ...
5
by: James Gregory | last post by:
#include <cstdlib> #include <cctype> #include <string> template <class Input_Iter> inline int IterToInt(Input_Iter& iter, const Input_Iter& lineEnd) { char tempArray = {0}; for (int i = 0;...
3
by: Marcin Kalicinski | last post by:
void f(const char *&text); Is this a const reference to char * or a reference to const char *? And how to write both of them? thank you, Marcin
2
by: Pavel | last post by:
I am writing software for an embedded application and here is the question. GCC would emit data declared like const char text = "abc"; to .rodata (i.e. "read only data") section. I can put this...
6
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
2
by: ragged_hippy | last post by:
Hi, If I have a method that has string reference as a parameter, what happens if I pass a const char* variable to this method? One thought is that a temporary string will be created in the...
2
by: wizofaus | last post by:
Given the following code: public class Test { static unsafe void StringManip(string data) { fixed (char* ps = data) ps = '$'; }
2
by: cablepuff | last post by:
template <typename ContainerType> ContainerType rsa_encrypt_list(const std::string&, const typename ContainerType::reference, const typename ContainerType::reference); const BigInteger...
5
by: Bob Doe | last post by:
I have a const static object. What is the right syntax to get a reference to the std::vector within the std::map variable?: class MyObj { public: ... std::map<std::string,...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.