Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > elementary question about setting up class

Reply
Thread Tools

elementary question about setting up class

 
 
pauldepstein@att.net
Guest
Posts: n/a
 
      12-01-2005
I am writing a program which looks at nodes which have coordinates
which are time-dependent.

So I have a class called node which contains the private member
declarations int date; int month; (I want to consider the month as
part of the information contained in the node, as well as the date.)

The node class also contains public member function void set_month(int)
which sets the month according to the date given.

The corresponding code is void node::set_month(int given_date)
{ int calendar = given_date % 365;
if(calendar <= 31) month = 0;

etc. etc. }


I want my default constructor to assume some basic assumptions about
the node.

My plan is for the default construction node::node() to call the
set_month member function

In other words, my node::node() function would contain the line of code

set_month(date);

Is this o.k. as a line of code? In fact, would it be o.k to write

node::node()
{ set_month(date); }


Is there a problem with this with regard to either style or legality.
It seems to solve my problem but a default constructor should (I think)
be the most basic type of function and it seems wrong for a default
constructor to call on another member function.

Or is my proposed approach o.k?

Thank you,

Paul Epstein

 
Reply With Quote
 
 
 
 
deane_gavin@hotmail.com
Guest
Posts: n/a
 
      12-01-2005

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I am writing a program which looks at nodes which have coordinates
> which are time-dependent.
>
> So I have a class called node which contains the private member
> declarations int date; int month; (I want to consider the month as
> part of the information contained in the node, as well as the date.)
>
> The node class also contains public member function void set_month(int)
> which sets the month according to the date given.
>
> The corresponding code is void node::set_month(int given_date)
> { int calendar = given_date % 365;
> if(calendar <= 31) month = 0;
>
> etc. etc. }
>
>
> I want my default constructor to assume some basic assumptions about
> the node.
>
> My plan is for the default construction node::node() to call the
> set_month member function
>
> In other words, my node::node() function would contain the line of code
>
> set_month(date);
>
> Is this o.k. as a line of code? In fact, would it be o.k to write
>
> node::node()
> { set_month(date); }
>
>
> Is there a problem with this with regard to either style or legality.
> It seems to solve my problem but a default constructor should (I think)
> be the most basic type of function and it seems wrong for a default
> constructor to call on another member function.
>
> Or is my proposed approach o.k?


By the time the code in the body of your constructor is called, all the
messy behind-the-scenes stuff (allocating memory, constructing data
members and base classes) has completed so it is perfectly safe and
legal to call a member function. A bit more to think about with virtual
member functions, but I assume that's not relevant here.

If you need identical functionality in the constructor and the member
function then it is right that the constructor calls the member
function.

Gavin Deane

 
Reply With Quote
 
 
 
 
wittempj@hotmail.com
Guest
Posts: n/a
 
      12-01-2005
You should initialize date with a value, otherwise a call as you
suggest will not initialize month predictably - so make the default
constructor e.g.

node::node():date(1)
{ set_month(date); }

 
Reply With Quote
 
deane_gavin@hotmail.com
Guest
Posts: n/a
 
      12-01-2005
(E-Mail Removed) wrote:
> (E-Mail Removed) wrote:
> > I am writing a program which looks at nodes which have coordinates
> > which are time-dependent.
> >
> > So I have a class called node which contains the private member
> > declarations int date; int month; (I want to consider the month as
> > part of the information contained in the node, as well as the date.)
> >
> > The node class also contains public member function void set_month(int)
> > which sets the month according to the date given.


<snip>

> > My plan is for the default construction node::node() to call the
> > set_month member function
> >
> > In other words, my node::node() function would contain the line of code
> >
> > set_month(date);
> >
> > Is this o.k. as a line of code? In fact, would it be o.k to write
> >
> > node::node()
> > { set_month(date); }


<snip>

> By the time the code in the body of your constructor is called, all the
> messy behind-the-scenes stuff (allocating memory, constructing data
> members and base classes) has completed so it is perfectly safe and
> legal to call a member function. A bit more to think about with virtual
> member functions, but I assume that's not relevant here.


I was thinking about the principle of what you are doing and missed
some of the detail of your actual code. As somone else has correctly
pointed out, you will need to initialise the member variable date
before the constructor passes it to the set_month member function. The
member variable exists by the time the body of your constructor is
executed, but being a built-in type, it is uninitialised. Passing it to
set_month in that state would be undefined bahaviour.

Gavin Deane

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-01-2005
* (E-Mail Removed):
> I am writing a program which looks at nodes which have coordinates
> which are time-dependent.
>
> So I have a class called node which contains the private member
> declarations int date; int month; (I want to consider the month as
> part of the information contained in the node, as well as the date.)
>
> The node class also contains public member function void set_month(int)
> which sets the month according to the date given.
>
> The corresponding code is void node::set_month(int given_date)
> { int calendar = given_date % 365;
> if(calendar <= 31) month = 0;
>
> etc. etc. }


If you can compute the month from the date, provide a member function to
extract the month.

Anything else is premature optimization that causes trouble.



> I want my default constructor to assume some basic assumptions about
> the node.
>
> My plan is for the default construction node::node() to call the
> set_month member function
>
> In other words, my node::node() function would contain the line of code
>
> set_month(date);
>
> Is this o.k. as a line of code? In fact, would it be o.k to write
>
> node::node()
> { set_month(date); }


Here date is not yet initialized; see above.



> Is there a problem with this with regard to either style or legality.


Yes, see above.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
pauldepstein@att.net
Guest
Posts: n/a
 
      12-01-2005

(E-Mail Removed) wrote:
> You should initialize date with a value, otherwise a call as you
> suggest will not initialize month predictably - so make the default
> constructor e.g.
>
> node::node():date(1)
> { set_month(date); }


I am not familiar with this code. Is this equivalent to node::node()
{ date = 1;
set_month(date); } ?

Furthermore, if I write my code like this. As I change the date, is
there a danger that set_month(date) will operate on the default date of
1 rather than than the newly changed date?

In other words, suppose my set_month works properly

Suppose I have

node::node()
{date = 1; set_month(date); }

int main (void)

{ example node;

[code here via accessor functions to set the date of example to be
73];

... more code;

}

Will set_month know to operate on 73 instead of 1? (I think so -- I
think that class members behave a bit like static variables in a loop.
It's the last value that's important, not the value declared
initially.)

Am I right on this?

I think someone on the thread is right that I should not use the month
variable at all -- just have a month function that returns an integer,
and then refer to example.set_month() whenever I need it.

Thank you,

Paul Epstein

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      12-01-2005
(E-Mail Removed) wrote:
> (E-Mail Removed) wrote:
>> You should initialize date with a value, otherwise a call as you
>> suggest will not initialize month predictably - so make the default
>> constructor e.g.
>>
>> node::node():date(1)
>> { set_month(date); }

>
> I am not familiar with this code. Is this equivalent to node::node()
> { date = 1;
> set_month(date); } ?

No. One is initialization, the other is assignment.

>
> Furthermore, if I write my code like this. As I change the date, is
> there a danger that set_month(date) will operate on the default date of
> 1 rather than than the newly changed date?


I'm confused. Constructors only run at initialization time...if you
call methods after the object is initialized, you don't have to worry
about the constructor body anymore.
 
Reply With Quote
 
pauldepstein@att.net
Guest
Posts: n/a
 
      12-01-2005

Ron Natalie wrote:

> >> node::node():date(1)
> >> { set_month(date); }

> >
> > I am not familiar with this code. Is this equivalent to node::node()
> > { date = 1;
> > set_month(date); } ?

> No. One is initialization, the other is assignment.
>


I do understand initialization but not assignment. And yes, I have
tried to google around. If anyone would like to give a web link that
explains this type of assignment (or provide an explanation
themselves), that would be great.

Or I can be reached directly via email at (E-Mail Removed) or by
yahoo messenger -- ID: pauldepstein

Thank you,

Paul Epstein

 
Reply With Quote
 
wittempj@hotmail.com
Guest
Posts: n/a
 
      12-02-2005
For more information on how to initialize members in a constructor see
http://www.parashift.com/c++-faq-lit....html#faq-10.6

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using only the values returned by functions -- elementary question pauldepstein@att.net C++ 6 01-09-2006 10:03 PM
Using only the values returned by functions -- elementary question pauldepstein@att.net C++ 0 01-09-2006 04:32 AM
class organization (very elementary) pauldepstein@att.net C++ 3 12-06-2005 03:03 PM
Elementary question on const Last Timer C++ 5 01-30-2005 08:54 PM
REALLY elementary question about low-light photography dakota7 Digital Photography 15 11-14-2003 12:44 AM



Advertisments