| View previous topic :: View next topic |
| Author |
Message |
Jason Guest
|
Posted: Wed Oct 19, 2005 8:49 am Post subject: const string & and static char array |
|
|
Hi,
I didn't understand how come I can call Foo::m1 with a static
char[] parameter. I am using Visual C++ 6.0 and never tried this on
another compiler. Is this a compiler bug?
class Foo
{
public:
void m1(const string& str);
};
class Settings
{
public:
static char Setting1[256];
};
// in the code
Foo foo;
foo.m1(Settings::Setting1); // why there is no error? is there an
implicit conversion?
Jason
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Thu Oct 20, 2005 11:14 am Post subject: Re: const string & and static char array |
|
|
Jason wrote:
| Quote: | class Foo {
public:
void m1(const string& str);
};
class Settings {
public:
static char Setting1[256];
};
// in the code
Foo foo;
foo.m1(Settings::Setting1); // why there is no error?
// is there an implicit conversion?
|
Yes. See the standard, 21.3.1/5 -- one of the constructors of
string is:
basic_string(const charT* s, const Allocator&A = Allocator());
So your character array is converted to a char* for the first
parameter, and the string's default allocator is being used as
the second.
Why does that surprise you?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Sebastian Redl Guest
|
Posted: Thu Oct 20, 2005 11:21 am Post subject: Re: const string & and static char array |
|
|
Jason wrote:
| Quote: | // in the code
Foo foo;
foo.m1(Settings::Setting1); // why there is no error? is there an
implicit conversion?
|
There is. char[] is implicitely converted to const char *. std::string has a
constructor that takes a const char *, which is used as a conversion
constructor to construct a temporary std::string object that the reference
gets bound to.
--
Sebastian Redl
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Thu Oct 20, 2005 12:06 pm Post subject: Re: const string & and static char array |
|
|
Jason <bumperman_jc (AT) hotmail (DOT) com> wrote:
| Quote: | Hi,
I didn't understand how come I can call Foo::m1 with a static
char[] parameter. I am using Visual C++ 6.0 and never tried this on
another compiler. Is this a compiler bug?
|
It's not a bug.
| Quote: | class Foo
{
public:
void m1(const string& str);
};
class Settings
{
public:
static char Setting1[256];
};
// in the code
Foo foo;
foo.m1(Settings::Setting1); // why there is no error? is there an
// implicit conversion?
|
There are built-in implicit conversions from char[n] (for any size n)
to char * and from char * to const char *, and std::string has a
constructor that takes const char * and isn't declared explicit. So
there is an implicit conversion from char[256] to std::string.
--
Ben Hutchings
Having problems with C++ templates? Your questions may be answered by
<http://womble.decadentplace.org.uk/c++/template-faq.html>.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
unspammable@gmail.com Guest
|
Posted: Thu Oct 20, 2005 12:56 pm Post subject: Re: const string & and static char array |
|
|
std::string has a copy constructor that accepts a char pointer.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Thu Oct 20, 2005 1:57 pm Post subject: Re: const string & and static char array |
|
|
On 19 Oct 2005 04:49:30 -0400, "Jason" <bumperman_jc (AT) hotmail (DOT) com>
wrote in comp.lang.c++.moderated:
| Quote: | Hi,
I didn't understand how come I can call Foo::m1 with a static
char[] parameter. I am using Visual C++ 6.0 and never tried this on
another compiler. Is this a compiler bug?
class Foo
{
public:
void m1(const string& str);
};
class Settings
{
public:
static char Setting1[256];
};
// in the code
Foo foo;
foo.m1(Settings::Setting1); // why there is no error? is there an
implicit conversion?
Jason
|
There is a constructor for std::string that accepts a C string as a
source parameter. This constructor is being invoked to create a
temporary std::string, which is bound to the constant reference for
the duration of the function call, and destroyed when the function
returns.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carlos Moreno Guest
|
Posted: Thu Oct 20, 2005 7:36 pm Post subject: Re: const string & and static char array |
|
|
[email]unspammable (AT) gmail (DOT) com[/email] wrote:
| Quote: | std::string has a copy constructor that accepts a char pointer.
|
No it doesn't.
If it accepts a char pointer, then it is *not* a copy constructor.
Carlos
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|