473,388 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

I canīt understand it!

Hi,

Is it possible a implicit cast from const char * to bool?
With this program:

class Foo
{
public:
pru (string param){ cout <<"string"<<endl;}
pru (bool param){ cout <<"bool";<<endl}
};

int main()
{
pru * p1=new pru("string");
string pp = "string";
pru * p2=new pru(pp);
pru * p3=new pru(true);
}

I get this results:

bool
string
bool

So in the first new, when I pass a const char type the compiler is making a implicit cast to bool. Anybody can give me an explanation?

TIA.
Jose.
Jul 22 '05 #1
2 1017
Jose Alberto wrote:
Hi,

Is it possible a implicit cast from const char * to bool?
With this program:

class Foo
{
public:
pru (string param){ cout <<"string"<<endl;}
pru (bool param){ cout <<"bool";<<endl}
};

int main()
{
pru * p1=new pru("string");
string pp = "string";
pru * p2=new pru(pp);
pru * p3=new pru(true);
}

I get this results:

bool
string
bool

So in the first new, when I pass a const char type the compiler is
making a implicit cast to bool. Anybody can give me an explanation?


Since the is no constructor that takes a const char* as input argument, so
a conversion has to made when you try to construct a Foo object using
"string" as argument for the constructor. A const char* can be converted to
both a bool and a string. In this case the conversion to bool wins.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl

Jul 22 '05 #2
Jose Alberto wrote:
Hi,

Is it possible a implicit cast from const char * to bool?
Yes.
With this program:

class Foo
{
public:
pru (string param){ cout <<"string"<<endl;}
pru (bool param){ cout <<"bool";<<endl}
};

int main()
{
pru * p1=new pru("string");
string pp = "string";
pru * p2=new pru(pp);
pru * p3=new pru(true);
}

I get this results:

bool
string
bool

So in the first new, when I pass a const char type the compiler is
making a implicit cast to bool. Anybody can give me an explanation?


You can write something like:

const char* p = whatever;
if (p)
do_something();
else
do_something_else();

But an if statement wants a bool, so the pointer p must be implicitly
convertible to bool. So p is converted to a bool with the value false
if it's a null pointer, or true otherwise.
So in your above example, both overloads are candidates for the call.
The bool overload is chosen because bool is a built-in type, while
string is a user-defined type, and conversions to built-in types are
always preferred.
You have to add another overload for const char*.

Jul 22 '05 #3

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

Similar topics

10
by: Christopher H. Laco | last post by:
Long story longer. I need to get web user input into a backend system that a) only grocks single byte encoding, b) expectes the data transer to be 1 bytes = 1 character, and c) uses the HP Roman-6...
10
by: forgotten field | last post by:
Hi,how are you? I have been studying C++ by myself, but recently I am having a real problem. I am learning about the basic usage of a doubly linked list using polymorphism. However, I have got the...
7
by: Robert Allan Schwartz | last post by:
Why do I get a syntax error below? I don't see why volatile works but unsigned does not work. I'm not looking for an answer of the form, "Because the Standard says so", or "Because the C++...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
14
by: jlemay | last post by:
if you look at http://www.emrose.net/contact1.html the last field is a textarea field and I can't make it align with the other text fields. it's pretty close, but this comes from trial and...
2
by: DC | last post by:
Hi, I need to asynchronously read from a network (TCP) stream, and I am having trouble with retrieving whole blocks; I get a break in the data block every 1460 bytes which relates to network...
9
by: tshad | last post by:
I have an example I copied from "programming asp.net" (o'reilly) and can't seem to get the Sub (writefile) to execute. It displays all the response.write lines that are called directly, but not...
24
by: Richard Aubin | last post by:
I'm really new to vb.net programming and programming in general. I would like to teach myself on how to program effectively and I have the financial and time resources to do so. Can I anyone...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.