pauldepstein@att.net wrote:[color=blue]
> 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?[/color]
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