elementary question about setting up class

Discussion in 'C++' started by pauldepstein@att.net, Dec 1, 2005.

  1. Guest

    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
     
    , Dec 1, 2005
    #1
    1. Advertising

  2. Guest

    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
     
    , Dec 1, 2005
    #2
    1. Advertising

  3. Guest

    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); }
     
    , Dec 1, 2005
    #3
  4. Guest

    wrote:
    > 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
     
    , Dec 1, 2005
    #4
  5. * :
    > 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?
     
    Alf P. Steinbach, Dec 1, 2005
    #5
  6. Guest

    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
     
    , Dec 1, 2005
    #6
  7. Ron Natalie Guest

    wrote:
    > 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.
     
    Ron Natalie, Dec 1, 2005
    #7
  8. Guest

    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 or by
    yahoo messenger -- ID: pauldepstein

    Thank you,

    Paul Epstein
     
    , Dec 1, 2005
    #8
  9. Guest

    , Dec 2, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Last Timer

    Elementary question on const

    Last Timer, Jan 30, 2005, in forum: C++
    Replies:
    5
    Views:
    365
    Mike Wahler
    Jan 30, 2005
  2. Replies:
    3
    Views:
    323
    Neil Cerutti
    Dec 6, 2005
  3. Replies:
    6
    Views:
    293
    Marcus Kwok
    Jan 9, 2006
  4. Replies:
    0
    Views:
    282
  5. Steven W. Orr

    elementary tuple question. (sorry)

    Steven W. Orr, Apr 5, 2007, in forum: Python
    Replies:
    3
    Views:
    358
Loading...

Share This Page