Original Post
I recently encountered a piece of code like the following (not saying anything about whether it is good or not):
[source lang="cpp"]class Ptr
{
public:
Ptr(void* ptr = NULL, bool owned= true);
...
}
Ptr ptr(false);[/source]
This seems to compile pretty well, and the false is implicitly converted to a null pointer! In case you give true as argument to this constructor, you get a compile error.
Now my question is: how can you prefend that the bool is converted automatically to a null pointer. I tried an explicit constructor with only one bool argument. But that made it impossible to send NULL as pointer to the first.
[source lang="cpp"]class Ptr
{
public:
Ptr(void* ptr = NULL, bool owned= true);
...
}
Ptr ptr(false);[/source]
This seems to compile pretty well, and the false is implicitly converted to a null pointer! In case you give true as argument to this constructor, you get a compile error.
Now my question is: how can you prefend that the bool is converted automatically to a null pointer. I tried an explicit constructor with only one bool argument. But that made it impossible to send NULL as pointer to the first.