A type conversion question

T

Tao Lu

class A
{
pub:
A(const char*); //1
A(char *); //2
} ;

if i do A a("blah");
which constructor should be called?

because "blah" is a char * const ...so I am not sure about the
conversion that would happen.

I have tested on both gcc and vc and gcc used cotr 1 while vc used 2.
What does the Spec say about this situation?

Thanks.
 
S

Siemel Naran

Tao Lu said:
class A
{
pub:
A(const char*); //1
A(char *); //2
} ;

if i do A a("blah");
which constructor should be called?

My guess is for A::A(const char *) because string literals in C++ have type
const char *.
 
R

Rolf Magnus

Siemel said:
My guess is for A::A(const char *) because string literals in C++ have
type const char *.

Actually, they are of type const char[number_of_characters+1]. But even
though both the conversion to const char* and to char* are allowed for
string literals, I think the former is to be used.
 

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

No members online now.

Forum statistics

Threads
474,166
Messages
2,570,901
Members
47,442
Latest member
KevinLocki

Latest Threads

Top