ARTICLE AD BOX
tl;dr: The pointer to your newly-created A is converted into a bool.
Here's a run-down of what happens as that line of your program runs:
Memory is allocated An unnamed A instance is constructed, with the constant true value, at the allocated memory. The pointer to the unnamed A is the (single) argument to the construction of data. The only available constructor is A::A(bool), and a pointer is implicitly-convertible to bool, so that's what happens; the converted value is true (since the pointer is not null). The constructor A::A(bool) runs for data, with the argument true.Let's add a printing instruction into your constructor, to see this in action:
#include <iostream> class A { private: const bool state; public: A(bool state) : state(state) { std::cout << "In constructor A::A(bool)\n"; } }; int main() { A data = new A(true); }If you build and run this program (godbolt.org), you get:
In constructor A::A(bool) In constructor A::A(bool)For an explanation of why C++ allows this conversion is the case, see:
Why is there an implicit type conversion from pointers to bool in C++?
(thanks @Richard for pointing to that question)
139k87 gold badges452 silver badges933 bronze badges
