I can´t understand it!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jose Alberto

    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"<<end l;}
    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.


  • Peter van Merkerk

    #2
    Re: I can´t understand it!

    Jose Alberto wrote:[color=blue]
    > Hi,
    >
    > Is it possible a implicit cast from const char * to bool?
    >
    >
    > With this program:
    >
    > class Foo
    > {
    > public:
    > pru (string param){ cout <<"string"<<end l;}
    > 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?[/color]

    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.merke rk(at)dse.nl



    Comment

    • Rolf Magnus

      #3
      Re: I can?t understand it!

      Jose Alberto wrote:
      [color=blue]
      > Hi,
      >
      > Is it possible a implicit cast from const char * to bool?[/color]

      Yes.
      [color=blue]
      > With this program:
      >
      > class Foo
      > {
      > public:
      > pru (string param){ cout <<"string"<<end l;}
      > 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?[/color]

      You can write something like:

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

      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*.

      Comment

      Working...