C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

const string & and static char array

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Jason
Guest





PostPosted: Wed Oct 19, 2005 8:49 am    Post subject: const string & and static char array Reply with 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


[ 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





PostPosted: Thu Oct 20, 2005 11:14 am    Post subject: Re: const string & and static char array Reply with quote



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





PostPosted: Thu Oct 20, 2005 11:21 am    Post subject: Re: const string & and static char array Reply with quote



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





PostPosted: Thu Oct 20, 2005 12:06 pm    Post subject: Re: const string & and static char array Reply with quote

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





PostPosted: Thu Oct 20, 2005 12:56 pm    Post subject: Re: const string & and static char array Reply with quote

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





PostPosted: Thu Oct 20, 2005 1:57 pm    Post subject: Re: const string & and static char array Reply with quote

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





PostPosted: Thu Oct 20, 2005 7:36 pm    Post subject: Re: const string & and static char array Reply with quote

[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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.