Conversion of false to object via const char * constructor

后端 未结 3 1016
广开言路
广开言路 2021-01-20 15:59

I have built the following minimal example:

class A
{
    public:
        A(const char *s);

    private:
        const char *p;
};

A::A(const char *s)
  :          


        
3条回答
  •  走了就别回头了
    2021-01-20 16:47

    For 1), false is of type bool, and it can be promoted to an int implicitely:

    • the type bool can be converted to int with the value false becoming ​0​ and true becoming 1.

    So, false is basically promoted to NULL/0 (or nullptr), which can be assigned to a pointer.

    For 2), §4.10 states that:

    A null pointer constant is an integer literal (2.14.2) with value zero or a prvalue of type std::nullptr_t.

    A null pointer constant can be converted to a pointer type; [...]

    Only a Null pointer constant can be converted to a pointer, and a null pointer constant is either an intergral with value 0 or std::nullptr_t. true (or 1 for that matter) aren't specified, and thus they can't be converted to a pointer.

提交回复
热议问题