473,398 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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
4 1325
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
2
by: Anders Borum [.NET/C# MCP] | last post by:
Hello! With C# 2.0 coming up, I was wondering what your thoughts are regarding the introduction of access modifiers on set accessors. Personally, I like the first example, because of its...
10
by: George G. | last post by:
Hi there, I am busy writing a new asp.net application and I am reusing some of my existing asp functions and methods in a user control. I need access to session, request and response in some of...
1
by: Mark Kamoski | last post by:
Hi-- Please help. What is the default access modifier value for a Sub or a Function when one has not explicitly been set. In MSDN, in "Access Types", it states... "If no access modifier...
12
by: Chris | last post by:
Hi I am trying to create a base class with the Protected keyword Protected MustInherit Class MyBaseClas .. .. End Clas And I got an error saying something like the Protected keyword...
2
by: Nigel Molesworth | last post by:
I'm doing a Java course. Having gone through the use of accessor (setter and getter) methods for instance variables, it goes on to say: "For reasons that cannot be elaborated upon here, it is...
1
by: muler | last post by:
"If an instance method declaration includes the sealed modifier, it must also include the override modifier." The C# Programming Language, § 10.5.5 Sealed Methods Why is this? Thanks,...
1
by: muler | last post by:
"If an instance method declaration includes the sealed modifier, it must also include the override modifier." The C# Programming Language, § 10.5.5 Sealed Methods Why is this? Thanks,...
3
by: Yan | last post by:
Hi, I don't seem to be able to use for_each if it should replace a 'for' loop in a method (constructor in my case) and inside that 'for' loop a class member variable is being accessed. The...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.