467,879 Members | 1,309 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,879 developers. It's quick & easy.

Using a modifier method inside the constructor

The code is taken from another thread:
#include <iostream>

using std::cout;
using std::ostream;

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::ostream& operator<<( std::ostream& 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?

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #1
  • viewed: 1191
Share:
4 Replies
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.

Jul 23 '05 #2

"Ioannis Vranos" <iv*@remove.this.grad.com> wrote in message
news:1110510959.80224@athnrd02...
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
Jul 23 '05 #3
Howard wrote:
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?

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.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #4

"Ioannis Vranos" <iv*@remove.this.grad.com> wrote in message
news:1110562822.83929@athnrd02...
Howard wrote:
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.


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
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by Daniel | last post: by
2 posts views Thread by Anders Borum [.NET/C# MCP] | last post: by
2 posts views Thread by Nigel Molesworth | last post: by
3 posts views Thread by Yan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.