elementary question about setting up class

P

pauldepstein

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
 
D

deane_gavin

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
 
W

wittempj

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); }
 
D

deane_gavin

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
 
A

Alf P. Steinbach

* (e-mail address 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.
 
P

pauldepstein

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
 
R

Ron Natalie

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.
 
P

pauldepstein

Ron said:
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 address removed) or by
yahoo messenger -- ID: pauldepstein

Thank you,

Paul Epstein
 

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
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top