type conversion in C++

#include

using namespace std;

class test
{
private:
int value;

public:
test():value(0) { }
test(int v):value(v) { }
explicit test(const char *ch):value(*ch) { }
operator int() { return value; }
};

int main()
{
test t = 3;
// test t2 = static_cast(“a”); // Compile Time Error!
test t2 = static_cast(“a”);

int val1 = t;
int val2 = t2;

cout << val1 << endl; cout << val2 << endl; } [/code]

Similar Posts:

Comments 2