Using a modifier method inside the constructor

I

Ioannis Vranos

The code is taken from another thread:


#include <iostream>

using std::cout;
using std::eek: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::eek:stream& operator<<( std::eek: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?
 
P

Phil Staite

Ioannis Vranos wrote:

Question: Doesn't the use of assign() inside the constructor invoke
undefined behaviour, since the object is still in the construction phase?

No, since the instance data (variables) such as Var are all constructed
by the time you reach the opening { of the body of the constructor. You
can (and usually should) use an "init list" to initialize instance data
before the body of the constructor even runs. In this case it isn't
that important but it is a good habit to get into. Something like:

Var_T( const int& i = int() ) : Var(0)
{
assign( i );
}

That way, even if something goes wrong in the body of the constructor at
least the instance data is in a known state.
 
H

Howard

Ioannis Vranos said:
The code is taken from another thread:


class Var_T
{
public:
Var_T( const int& i = int() )

Not an answer to your question, but...

That's confusing. What's the parameter list do? I'm guessing it
default-constructs a temporary integer if none is provided, which gets bound
to the const reference? Does that make i get the value 0 as part of the
default construction? If so, and if it's possible in the syntax, I think
I'd have made that "= int(0)" just to be obvious what the default initial
value is going to be.

Or the whole thing could have been avoided by just having an int instead of
a const int reference. There's no more overhead for an int than there is
for a reference, so why use the reference?

-Howard
 
I

Ioannis Vranos

Howard said:
Not an answer to your question, but...

That's confusing. What's the parameter list do? I'm guessing it
default-constructs a temporary integer if none is provided, which gets bound
to the const reference?

Yes.


Does that make i get the value 0 as part of the
default construction?


It gets (points to) a temporary with the initial value 0.


If so, and if it's possible in the syntax, I think
I'd have made that "= int(0)" just to be obvious what the default initial
value is going to be.


Yes or directly:

Var_T( const int& i = 0 )


Myself would have made it:

Var_T( const int i = 0 )


He just used the default parameters of templates notion, that is used
extensively in the standard library (for example in vectors):


Var_T( const T &arg= T() )


Or the whole thing could have been avoided by just having an int instead of
a const int reference. There's no more overhead for an int than there is
for a reference, so why use the reference?


Because he wanted to, I guess. :) This style makes sense in generic
facilities (templates), where this works for both built in types and
user-defined types.
 
H

Howard

Ioannis Vranos said:
Because he wanted to, I guess. :) This style makes sense in generic
facilities (templates), where this works for both built in types and
user-defined types.

Oh, I see now where you said this was from another thread, sorry. :)
And thanks... I see where that could be a useful method with templates.

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,660
Latest member
vidotip479

Latest Threads

Top