469,645 Members | 1,084 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

A question about the safe bool idiom

I have read the safe bool idiom, and the author
give four defect implements. But I can't find any
difference between the void* and nested_class*
solution.

// operator void* version
class Testable {
bool ok_;
public:
explicit Testable(bool b=true):ok_(b) {}

operator void*() const {
return ok_==true ? this : 0;
}
};

// nested class version
class Testable {
bool ok_;
public:
explicit Testable(bool b=true):ok_(b) {}

class nested_class {};

operator const nested_class*() const {
return ok_ ? reinterpret_cast<const nested_class*>(this) : 0;
}
};
Cound anybody here tell me what is the differences between
the operator void* version and nested class version?

Thanks in advance!
Dec 17 '06 #1
1 1372
Bo Yang wrote:
I have read the safe bool idiom, and the author
give four defect implements. But I can't find any
difference between the void* and nested_class*
solution.
Cound anybody here tell me what is the differences between
the operator void* version and nested class version?
void * allows to call operator delete without any warnings. Nested and
not defined class (1) does not call delete, (2 in my case) - emits
warning.

Dec 18 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.