Created
April 18, 2012 11:21
-
-
Save mloskot/2412967 to your computer and use it in GitHub Desktop.
Smart pointer example with mis-use of bool conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Interesting issue about conversion operators in C++ | |
//http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1592.pdf | |
#include <iostream> | |
template <class T> | |
class Ptr | |
{ | |
// stuff | |
public: | |
operator bool() const | |
{ | |
if( rawptr_ ) | |
return true; | |
else | |
return false; | |
} | |
private: | |
T * rawptr_; | |
}; | |
int main() | |
{ | |
Ptr<int> p1; | |
Ptr<float> p2; | |
std::cout << "p1 + p2 = " << p1 + p2 << std::endl; // prints 0, 1, or 2 | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment