I
Ioannis Vranos
The code is taken from another thread:
#include <iostream>
using std::cout;
using std:stream;
class Range_Error
{
public:
Range_Error()
: message( "raised \"Out of Range\"" ) {}
const char* what() const { return message; }
private:
const char* message;
};
class Var_T
{
public:
Var_T( const int& i = int() )
{
assign( i );
}
Var_T& operator++( )
{
assign( ++Var );
return *this;
}
bool assign( const int& i )
{
if ( i >= 0 && i <= 100 )
Var = i;
else throw Range_Error();
return true;
}
friend std:stream& operator<<( std:stream& out, const Var_T&
obj )
{
out << obj.Var;
return out;
}
private:
int Var;
};
int main()
{
Var_T Var( 0 );
try
{
for ( int i = 0; i <= 100; ++i ) // range check error expected
++Var;
cout << Var << '\n';
}
catch( Range_Error ERR )
{
cout << ERR.what() << '\n';
}
return 0;
}
Question: Doesn't the use of assign() inside the constructor invoke
undefined behaviour, since the object is still in the construction phase?
#include <iostream>
using std::cout;
using std:stream;
class Range_Error
{
public:
Range_Error()
: message( "raised \"Out of Range\"" ) {}
const char* what() const { return message; }
private:
const char* message;
};
class Var_T
{
public:
Var_T( const int& i = int() )
{
assign( i );
}
Var_T& operator++( )
{
assign( ++Var );
return *this;
}
bool assign( const int& i )
{
if ( i >= 0 && i <= 100 )
Var = i;
else throw Range_Error();
return true;
}
friend std:stream& operator<<( std:stream& out, const Var_T&
obj )
{
out << obj.Var;
return out;
}
private:
int Var;
};
int main()
{
Var_T Var( 0 );
try
{
for ( int i = 0; i <= 100; ++i ) // range check error expected
++Var;
cout << Var << '\n';
}
catch( Range_Error ERR )
{
cout << ERR.what() << '\n';
}
return 0;
}
Question: Doesn't the use of assign() inside the constructor invoke
undefined behaviour, since the object is still in the construction phase?