Re: A question about variables declaration in C++

Discussion in 'C++' started by Chris \( Val \), Jul 30, 2003.

  1. "John Harrison" <> wrote in message
    news:bg7ok1$l6rfo$-berlin.de...
    |
    | "Dog" <> wrote in message
    | news:bg7n3d$irl$...
    | > Hi,
    | > A question about variables declaration.
    | > There're 3 variables declared in a class.
    | > Which one is better? Why?
    | >
    | > (A)
    | > {
    | > int i1;
    | > double d;
    | > int i2;
    | > }
    | >
    | > (B)
    | > {
    | > int i1;
    | > int i2;
    | > double d;
    | > }
    | >
    | > (C)
    | > {
    | > double d;
    | > int i1;
    | > int i2;
    | > }
    | >
    | > I've no idea about this.
    | > Could you please provide me some hints.
    | >
    | > Thanks.
    | >
    |
    | They are all fine, its a stupid question, possibly trying to say something
    | about the packing of member variables in a class. But that is a compiler
    | specific topic, so the correct answer for one compiler will be the wrong
    | answer for another. In any case it makes very little difference.

    Hi John.

    I value your contributions in c.l.c++, however, I must disagree
    with your strong response. There really are no stupid questions
    in C++ (especially for newbie's).

    However, I don't believe the OP even mentioned anything about
    a performance issue, that everyone seems to have assumed to be
    the case of the question.

    There *may* be some implementation dependant performance issues,
    however, no one has yet to mention that there *is* a difference
    with the order data members are declared.

    That is, they are *initialised* in a specific order, and that
    *does* matter.

    For example:

    # include <iostream>
    # include <ostream>

    class Base
    {
    private:
    int First;
    int Second;
    public:
    Base( const int& N ) : Second( N ), First( Second )
    {
    std::cout << First << std::endl;
    }
    };


    int main()
    {
    Base B( 10 );

    return 0;
    }

    It is not the order that they are initialised in the
    initialiser list that matters here, it is the order
    in which these data members are declared.

    As you can see by the output, you can create a nasty
    bug for yourself, that is difficult to track down.

    Cheers.
    Chris Val
     
    Chris \( Val \), Jul 30, 2003
    #1
    1. Advertising

  2. Chris \( Val \)

    Howard Guest

    (Sounds like another homework question to me!)

    "Chris ( Val )" <> wrote in message
    news:bg8bn1$lf9fi$-berlin.de...

    > However, I don't believe the OP even mentioned anything about
    > a performance issue, that everyone seems to have assumed to be
    > the case of the question.


    "Packing" is not performance-related, but space-related. Re-arranging the
    variables may, in certain compiler and operating system combinations, allow
    less space to be used, due to alignment issues. (Then again, it may not.)
    Performance is a speed issue, and may or may not be affected by such a small
    difference in size as could possibly occur here.

    >
    > There *may* be some implementation dependant performance issues,
    > however, no one has yet to mention that there *is* a difference
    > with the order data members are declared.
    >
    > That is, they are *initialised* in a specific order, and that
    > *does* matter.
    >


    But the example is not using user-defined types, but simple built-in types,
    and there is initialization taking place, simply the allocation of enough
    memory space to hold them. So order does not matter here.

    Perhaps the OP was told in his class that the teacher wants their variables
    declared in a certain manner? I can think of no other reason why it would
    matter the order in which these *particular* variables are declared could
    possibly matter. Certainly, they do not need to be in alphabetic order, nor
    is there any particular reason to group int's together as opposed to having
    an int then a double then another int. (But the variable names themselves
    ought to be more descriptive of their function...in a real world case at
    least.)

    If this actual curiosity on the part of the OP, then the answer is that it
    doesn't really matter. If it's a homework question, then I agree that it IS
    a stupid question! :)

    -Howard
     
    Howard, Jul 30, 2003
    #2
    1. Advertising

  3. Chris \( Val \)

    Ben Guest

    Chris ( Val ) wrote:
    > That is, they are *initialised* in a specific order, and that
    > *does* matter.


    But only the bare bones was given in the question so the below can't be
    what was being got at I would have thought.

    regards,
    Ben

    >
    > For example:
    >
    > # include <iostream>
    > # include <ostream>
    >
    > class Base
    > {
    > private:
    > int First;
    > int Second;
    > public:
    > Base( const int& N ) : Second( N ), First( Second )
    > {
    > std::cout << First << std::endl;
    > }
    > };
    >
    >
    > int main()
    > {
    > Base B( 10 );
    >
    > return 0;
    > }
    >
    > It is not the order that they are initialised in the
    > initialiser list that matters here, it is the order
    > in which these data members are declared.
    >
    > As you can see by the output, you can create a nasty
    > bug for yourself, that is difficult to track down.
    >
    > Cheers.
    > Chris Val
    >
    >


    --
    BTW. I can be contacted at Username:newsgroup4.replies.benaltw
    Domain:xoxy.net
     
    Ben, Jul 30, 2003
    #3
  4. >
    > Hi John.
    >
    > I value your contributions in c.l.c++, however, I must disagree
    > with your strong response. There really are no stupid questions
    > in C++ (especially for newbie's).


    I certainly wasn't trying to suggest that the OP was being stupid, apologies
    if I gave that impression. I thought it fairly obvious that the OP was
    repeating a question that s/he had been asked.

    >
    > However, I don't believe the OP even mentioned anything about
    > a performance issue, that everyone seems to have assumed to be
    > the case of the question.
    >
    > There *may* be some implementation dependant performance issues,
    > however, no one has yet to mention that there *is* a difference
    > with the order data members are declared.
    >
    > That is, they are *initialised* in a specific order, and that
    > *does* matter.
    >


    It does, but I doubt this was the intention behind the question in this
    case. If it was then the class quoted should have included some constructors
    to illustrated the difference. This is off topic in comp.lang.c as well.

    [snip]

    john
     
    John Harrison, Jul 31, 2003
    #4
  5. "John Harrison" <> wrote in message
    news:bgackj$mrn2a$-berlin.de...
    | >
    | > Hi John.
    | >
    | > I value your contributions in c.l.c++, however, I must disagree
    | > with your strong response. There really are no stupid questions
    | > in C++ (especially for newbie's).
    |
    | I certainly wasn't trying to suggest that the OP was being stupid, apologies
    | if I gave that impression. I thought it fairly obvious that the OP was
    | repeating a question that s/he had been asked.

    Hi John.

    I didn't think you were implying that the OP was stupid at all, it
    was the question I was referring too :).

    | > However, I don't believe the OP even mentioned anything about
    | > a performance issue, that everyone seems to have assumed to be
    | > the case of the question.
    | >
    | > There *may* be some implementation dependant performance issues,
    | > however, no one has yet to mention that there *is* a difference
    | > with the order data members are declared.
    | >
    | > That is, they are *initialised* in a specific order, and that
    | > *does* matter.
    | >
    |
    | It does, but I doubt this was the intention behind the question in this
    | case. If it was then the class quoted should have included some constructors
    | to illustrated the difference. This is off topic in comp.lang.c as well.

    My apologies, I didn't realise this was posted to c.l.c.
    I have removed c.l.c from this post.

    You might doubt it <g>, but if we use 'Occams Razor'...er..um...
    what is most likely the teacher is asking a newbie ? 'packing'
    vs 'what affect the order of member declaration may have' ?

    I would choose the latter, if any :).

    Cheers.
    Chris Val
     
    Chris \( Val \), Jul 31, 2003
    #5
  6. Chris ( Val ) wrote:

    >
    > "John Harrison" <> wrote in message
    > news:bgackj$mrn2a$-berlin.de...
    > | >
    > | > That is, they are *initialised* in a specific order, and that
    > | > *does* matter.
    > | >
    > |
    > | It does, but I doubt this was the intention behind the question in this
    > | case. If it was then the class quoted should have included some
    > | constructors to illustrated the difference. This is off topic in
    > | comp.lang.c as well.
    >
    > My apologies, I didn't realise this was posted to c.l.c.
    > I have removed c.l.c from this post.
    >
    > You might doubt it <g>, but if we use 'Occams Razor'...er..um...
    > what is most likely the teacher is asking a newbie ? 'packing'
    > vs 'what affect the order of member declaration may have' ?


    That depends entirely on the course that is being taught.
    If it is assumed that the students will write programs that have to be
    memory efficient (think of microchip programming), then the packing of a
    structure becomes very important.
    And also, i dont think that newbies are bitten that often by the
    initialisation order of class members in C++.

    >
    > I would choose the latter, if any :).


    And I would choose the former, but I would explain the subject before
    asking a question like the OP posted.

    >
    > Cheers.
    > Chris Val


    Bart v Ingen Schenau
    --
    a.c.l.l.c-c++ FAQ: http://www.snurse-l.org/acllc-c /faq.html
    a.c.l.l.c-c++ FAQ mirror: http://nullptr.merseine.nu:8080/acllcc .html
    c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    c.l.c++ FAQ: http://www.parashift.com/c -faq-lite/
     
    B. v Ingen Schenau, Jul 31, 2003
    #6
    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. Noah
    Replies:
    5
    Views:
    964
  2. Alex Vinokur
    Replies:
    4
    Views:
    595
    Jonathan Turkanis
    Apr 5, 2004
  3. Ovidesvideo
    Replies:
    4
    Views:
    519
    Andrey Tarasevich
    Dec 10, 2004
  4. kelvSYC
    Replies:
    6
    Views:
    7,242
    Richard Herring
    May 17, 2005
  5. Replies:
    4
    Views:
    1,084
    Richard Tobin
    Dec 12, 2006
Loading...

Share This Page