reg. constructor parameters

Discussion in 'C++' started by Pelle Beckman, Jul 13, 2005.

  1. Hi all,

    I have these classes (for a GUI library) that uses
    alot of variables that need to be set at c-tor time

    i.e.

    class Button : Widget {
    public:
    Button (int x, int y) : Widget (BUTTON),
    m_x (x),
    m_y (y),
    name (""),
    etc /.../

    Is it true that this generates better (meaning faster)
    code than doing this?

    Button (int x, int y) {
    this->m_x = x;
    this->m_y = y;
    etc /.../

    Thanks.

    -- Pelle
    Pelle Beckman, Jul 13, 2005
    #1
    1. Advertising

  2. Pelle Beckman wrote:
    > I have these classes (for a GUI library) that uses
    > alot of variables that need to be set at c-tor time
    >
    > i.e.
    >
    > class Button : Widget {
    > public:
    > Button (int x, int y) : Widget (BUTTON),
    > m_x (x),
    > m_y (y),
    > name (""),
    > etc /.../
    >
    > Is it true that this generates better (meaning faster)
    > code than doing this?
    >
    > Button (int x, int y) {
    > this->m_x = x;
    > this->m_y = y;
    > etc /.../


    Generally speaking, yes. Please refer to FAQ. Search for "assignment
    initialization".

    V
    Victor Bazarov, Jul 13, 2005
    #2
    1. Advertising

  3. Pelle Beckman

    Jaspreet Guest

    Pelle Beckman wrote:
    > Hi all,
    >
    > I have these classes (for a GUI library) that uses
    > alot of variables that need to be set at c-tor time
    >
    > i.e.
    >
    > class Button : Widget {
    > public:
    > Button (int x, int y) : Widget (BUTTON),
    > m_x (x),
    > m_y (y),
    > name (""),
    > etc /.../
    >
    > Is it true that this generates better (meaning faster)
    > code than doing this?
    >
    > Button (int x, int y) {
    > this->m_x = x;
    > this->m_y = y;
    > etc /.../
    >
    > Thanks.
    >
    > -- Pelle


    Yes almost always but there is just 1 exception to the rule. Pls go
    through
    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.6
    Jaspreet, Jul 14, 2005
    #3
  4. Pelle Beckman

    pven Guest

    pven, Jul 14, 2005
    #4
    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. Thirumalai
    Replies:
    2
    Views:
    5,314
    Peter Torr \(MS\)
    Jul 12, 2006
  2. csudha

    Reg. copy constructor

    csudha, Feb 20, 2005, in forum: C Programming
    Replies:
    11
    Views:
    579
    Michael Mair
    Feb 21, 2005
  3. Replies:
    7
    Views:
    333
    Gavin Deane
    May 17, 2006
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,223
  5. ingoweiss
    Replies:
    4
    Views:
    210
    Julian Turner
    May 12, 2006
Loading...

Share This Page