A C++ Exception to the Rules
#include <iostream>
class Example
{
private:
const char * ptr;
const bool freeable;
public:
Example(const char * optional):
ptr( optional ? optional : new char [80]),
freeable( optional ? false : true ) {}
~Example( ) { if (freeable) delete[] ptr; }
void ViewState(char ID) {
if (freeable)
std::cout << ID << " is Freeable" << std::endl;
else
std::cout << ID << " is not Freeable" << std::endl;
}
};
int main(int argc, char * argv[])
{
Example S(0) ;
Example T("Hello World") ;
S.ViewState('S') ;
T.ViewState('T') ;
return 0;
}
So S is freeable, T is not.


No comments yet. Leave a Comment