Stroustrup: chapter 4

Discussion in 'C++' started by arnuld, Mar 4, 2007.

  1. arnuld

    arnuld Guest

    i have 2 problems:

    1.) in section 4.2 he uses:

    bool is_open(File*)

    i want to know why he uses the pointer, instead of these 2:

    bool is_open(File) or bool is_open(File&)


    2.) section 4.9, Stroustrup says:

    double sqrt(double);
    enum Beer { Carlsberg, Tuborg, Thor };
    namespace NS { int a; }


    these are declarations only, as per my point of view but Stroustrup
    says these are definitions. how ?

    thanks
     
    arnuld, Mar 4, 2007
    #1
    1. Advertising

  2. arnuld

    benben Guest

    arnuld wrote:
    > i have 2 problems:
    >
    > 1.) in section 4.2 he uses:
    >
    > bool is_open(File*)
    >
    > i want to know why he uses the pointer, instead of these 2:
    >
    > bool is_open(File) or bool is_open(File&)


    The point is you can use a bool to indicate logical result. Whether the
    parameter should be passed by value, reference or pointer is the
    author's freedom.

    >
    >
    > 2.) section 4.9, Stroustrup says:
    >
    > double sqrt(double);
    > enum Beer { Carlsberg, Tuborg, Thor };
    > namespace NS { int a; }
    >
    >
    > these are declarations only, as per my point of view but Stroustrup
    > says these are definitions. how ?


    The exact text is quoted below:

    Of the declarations above, only

    double sqrt(double);
    extern int error_number;
    struct User;

    are not also definitions;...


    So clearly the author does NOT regard them as definitions. What makes
    you think the author says otherwise?


    >
    > thanks
    >


    Welcome

    Ben
     
    benben, Mar 4, 2007
    #2
    1. Advertising

  3. arnuld

    arnuld Guest

    Re: Stroustrup: chapter 4

    > On Mar 4, 10:17 am, benben <benhonghatgmaildotcom@nospam> wrote:
    > arnuld wrote:


    > > bool is_open(File) or bool is_open(File&)

    >
    > The point is you can use a bool to indicate logical result. Whether the
    > parameter should be passed by value, reference or pointer is the
    > author's freedom.


    ok, fine. BTW, as a newbie to C++, i feel, Stroustrup always
    complicates things.


    > > double sqrt(double);
    > > enum Beer { Carlsberg, Tuborg, Thor };
    > > namespace NS { int a; }

    >
    > > these are declarations only, as per my point of view but Stroustrup
    > > says these are definitions. how ?

    >
    > The exact text is quoted below:
    >
    > Of the declarations above, only
    >
    > double sqrt(double);
    > extern int error_number;
    > struct User;
    >
    > are not also definitions;...


    the last 3 you wrote, are the *only* ones that are "declarations
    without definitions". Stroustrup says the three declarations *i*
    posted are "declarations with definitions" and to me they look like
    "declarations without definitions".

    i want to know WHY the 3 declarations i posted are "declarations with
    defintions"?

    > So clearly the author does NOT regard them as definitions. What makes
    > you think the author says otherwise?


    that's about your 3 declaration . what about my 3.
     
    arnuld, Mar 4, 2007
    #3
  4. Re: Stroustrup: chapter 4

    On Mar 4, 11:40 am, "arnuld" <> wrote:
    > i have 2 problems:
    >
    > 1.) in section 4.2 he uses:
    >
    > bool is_open(File*)
    >
    > i want to know why he uses the pointer, instead of these 2:
    >
    > bool is_open(File) or bool is_open(File&)


    A file would normally be considered to have object semantics rather
    than value semantics so you have to use either a pointer or a
    reference. Copying the File object would then give two references to
    the file to be manipulated separately. Not what you want.

    It's probable that most people now would prefer the reference rather
    than the pointer as the argument isn't in any sense optional, but this
    also depends on how the File object is created - I don't have a copy
    of the book handy to see the context.

    >
    > 2.) section 4.9, Stroustrup says:
    >
    > double sqrt(double);
    > enum Beer { Carlsberg, Tuborg, Thor };
    > namespace NS { int a; }
    >
    > these are declarations only, as per my point of view but Stroustrup
    > says these are definitions. how ?
    >
    > thanks


    I think the standard terminology these days would be to say that the
    first (being a function prototype) is a declaration. The second is a
    definition because it defines the members of the enum.

    For the last I think you might say that the namespace is defined
    (notwithstanding that it can be opened again later) and that the
    integer a is declared. I suspect that some people would use different
    terminology for these last two.


    K
     
    =?iso-8859-1?q?Kirit_S=E6lensminde?=, Mar 4, 2007
    #4
  5. arnuld

    arnuld Guest

    Re: Stroustrup: chapter 4

    > On Mar 4, 10:59 am, "Kirit Sælensminde" <> wrote:

    > > bool is_open(File) or bool is_open(File&)

    >
    > A file would normally be considered to have object semantics rather
    > than value semantics so you have to use either a pointer or a
    > reference. Copying the File object would then give two references to
    > the file to be manipulated separately. Not what you want.


    ok

    > It's probable that most people now would prefer the reference rather
    > than the pointer as the argument isn't in any sense optional, but this
    > also depends on how the File object is created - I don't have a copy
    > of the book handy to see the context.


    a File, is simply a File on my hard disk.


    > > 2.) section 4.9, Stroustrup says:

    >
    > > double sqrt(double);
    > > enum Beer { Carlsberg, Tuborg, Thor };
    > > namespace NS { int a; }

    >
    > > these are declarations only, as per my point of view but Stroustrup
    > > says these are definitions. how ?

    >
    > > thanks

    >
    > I think the standard terminology these days would be to say that the
    > first (being a function prototype) is a declaration. The second is a
    > definition because it defines the members of the enum.
    >
    > For the last I think you might say that the namespace is defined
    > (notwithstanding that it can be opened again later) and that the
    > integer a is declared. I suspect that some people would use different
    > terminology for these last two.
    >
    > K


    quite confusing IMVHO
     
    arnuld, Mar 4, 2007
    #5
  6. Re: Stroustrup: chapter 4


    > ok, fine. BTW, as a newbie to C++, i feel, Stroustrup always
    > complicates things.
    >


    It's a good book in many ways, but not one to learn C++ from.
     
    John Harrison, Mar 4, 2007
    #6
  7. arnuld

    arnuld Guest

    Re: Stroustrup: chapter 4

    > On Mar 4, 2:27 pm, John Harrison <> wrote:
    > > ok, fine. BTW, as a newbie to C++, i feel, Stroustrup always
    > > complicates things.

    >
    > It's a good book in many ways, but not one to learn C++ from.


    i know very well.


    the only one/other good book available in my country is "Thinking in C+
    +" where Eckel makes heavy-use of C. i do not know C. i think i have
    discussed it enough here.
     
    arnuld, Mar 4, 2007
    #7
  8. arnuld

    Bo Persson Guest

    Re: Stroustrup: chapter 4

    arnuld wrote:
    >> On Mar 4, 10:59 am, "Kirit Sælensminde"
    >> <> wrote:

    >
    >>> bool is_open(File) or bool is_open(File&)

    >>
    >> A file would normally be considered to have object semantics rather
    >> than value semantics so you have to use either a pointer or a
    >> reference. Copying the File object would then give two references to
    >> the file to be manipulated separately. Not what you want.

    >
    > ok
    >
    >> It's probable that most people now would prefer the reference rather
    >> than the pointer as the argument isn't in any sense optional, but
    >> this
    >> also depends on how the File object is created - I don't have a copy
    >> of the book handy to see the context.

    >
    > a File, is simply a File on my hard disk.
    >
    >
    >>> 2.) section 4.9, Stroustrup says:

    >>
    >>> double sqrt(double);
    >>> enum Beer { Carlsberg, Tuborg, Thor };
    >>> namespace NS { int a; }

    >>
    >>> these are declarations only, as per my point of view but Stroustrup
    >>> says these are definitions. how ?

    >>
    >>> thanks

    >>
    >> I think the standard terminology these days would be to say that the
    >> first (being a function prototype) is a declaration. The second is a
    >> definition because it defines the members of the enum.
    >>
    >> For the last I think you might say that the namespace is defined
    >> (notwithstanding that it can be opened again later) and that the
    >> integer a is declared. I suspect that some people would use different
    >> terminology for these last two.
    >>
    >> K

    >
    > quite confusing IMVHO


    It is. :)

    Sometimes we need very precise wordning for a technical discussion. The
    difference between a declaration and a definition is at that level.

    Basically, a declaration introduces a name without spelling out much detail
    about it. For example:

    class x;

    declares that x is the name of a class. We don't know what kind of class.


    On the other hand

    class x
    {
    // functions and member variables goes here
    };

    is a definition of the class x. It not only tells us that x is a class, it
    also spells out the details.


    (And adding to the confusion, a definition without a preceding declaration,
    serves as BOTH a declaration and a definition, as it introduces the name AND
    gives all the details. Don't bother with this until later).



    Bo Persson
     
    Bo Persson, Mar 4, 2007
    #8
  9. arnuld

    Bo Persson Guest

    Re: Stroustrup: chapter 4

    arnuld wrote:
    >> On Mar 4, 10:17 am, benben <benhonghatgmaildotcom@nospam> wrote:
    >> arnuld wrote:

    >
    >>> bool is_open(File) or bool is_open(File&)

    >>
    >> The point is you can use a bool to indicate logical result. Whether
    >> the parameter should be passed by value, reference or pointer is the
    >> author's freedom.

    >
    > ok, fine. BTW, as a newbie to C++, i feel, Stroustrup always
    > complicates things.


    I believe that the THINGS just are complicated, and that Stroustrup tries
    hard to explain all the finer points. The book is not a gentle introduction,
    but a complete explanation of how the language works.

    Leaving out the details just wouldn't work here.


    Bo Persson
     
    Bo Persson, Mar 4, 2007
    #9
  10. arnuld

    arnuld Guest

    Re: Stroustrup: chapter 4

    > On Mar 5, 8:28 am, benben <benhonghatgmaildotcom@nospam> wrote:


    > 1)
    > double sqrt(double);
    >
    > This is a declaration since it introduces the name 'sqrt'; it is not a
    > definition since it doesn't tell you how it works. This is on the
    > declaration-but-not-definition list. Read carefully.
    >
    > 2)
    > enum Beer{Carlsberg, Tuborg, Thor};
    >
    > This is a declaration since it introduces the name 'Beer'; in addition,
    > it is also a definition as it gives every detail of what Beer is.
    >
    > 3)
    > namespace NS{int a;}
    >
    > This is a declaration since it introduces the name NS; it is also a
    > definition since it tells you what to expect in NS. Also, it declares
    > and defines NS::a.


    fine but why:

    char c; is a definition

    and

    extern char c; is not

    ?
     
    arnuld, Mar 5, 2007
    #10
  11. Re: Stroustrup: chapter 4

    On Mar 5, 1:19 pm, "arnuld" <> wrote:
    > > On Mar 5, 8:28 am, benben <benhonghatgmaildotcom@nospam> wrote:
    > > 1)
    > > double sqrt(double);

    >
    > > This is a declaration since it introduces the name 'sqrt'; it is not a
    > > definition since it doesn't tell you how it works. This is on the
    > > declaration-but-not-definition list. Read carefully.

    >
    > > 2)
    > > enum Beer{Carlsberg, Tuborg, Thor};

    >
    > > This is a declaration since it introduces the name 'Beer'; in addition,
    > > it is also a definition as it gives every detail of what Beer is.

    >
    > > 3)
    > > namespace NS{int a;}

    >
    > > This is a declaration since it introduces the name NS; it is also a
    > > definition since it tells you what to expect in NS. Also, it declares
    > > and defines NS::a.

    >
    > fine but why:
    >
    > char c; is a definition
    >
    > and
    >
    > extern char c; is not
    >
    > ?


    One way of thinking about the difference is that a declaration tells
    the compiler that something exists, but doesn't give the compiler
    enough information to do anything concrete. A definition gives the
    compiler something concrete to work on and should (in most cases) lead
    to some compiled code.

    The first (char c;), will reserve space on the stack but leave that
    stack position un-initialised. The second informs the compiler that a
    memory location has been reserved elsewhere, but doesn't tell it where
    to find it. Normally the linker would have to fix this up later.

    Enums, structs and class definitions are a little odd when looked at
    in this way as they don't always lead to any concrete code that the
    compiler produces.

    To be honest though there are things that everybody agrees on are
    either declarations or definitions and there are some things that are
    a bit more ambiguous. If telling the difference between declarations
    and definitions is your biggest problem learning C++ then you're doing
    pretty well :)


    K
     
    =?iso-8859-1?q?Kirit_S=E6lensminde?=, Mar 5, 2007
    #11
  12. arnuld

    Wayne Shu Guest

    Re: Stroustrup: chapter 4

    On 3ÔÂ5ÈÕ, ÏÂÎç2ʱ19·Ö, "arnuld" <geek.arn...@gmail..com> wrote:
    > > On Mar 5, 8:28 am, benben <benhonghatgmaildotcom@nospam> wrote:
    > > 1)
    > > double sqrt(double);

    >
    > > This is a declaration since it introduces the name 'sqrt'; it is not a
    > > definition since it doesn't tell you how it works. This is on the
    > > declaration-but-not-definition list. Read carefully.

    >
    > > 2)
    > > enum Beer{Carlsberg, Tuborg, Thor};

    >
    > > This is a declaration since it introduces the name 'Beer'; in addition,
    > > it is also a definition as it gives every detail of what Beer is.

    >
    > > 3)
    > > namespace NS{int a;}

    >
    > > This is a declaration since it introduces the name NS; it is also a
    > > definition since it tells you what to expect in NS. Also, it declares
    > > and defines NS::a.

    >
    > fine but why:
    >
    > char c; is a definition

    because it allocates storage for the variable c.
    >
    > and
    >
    > extern char c; is not

    but this statement is an extern declaration ,and doesn't allocate
    storage.
    BTW, if the extern declaration has an initializer, it becomes a
    definition.
    e.g. extern char c = 'a'; // a definition.
    >
    > ?- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
    >
    > - ÏÔʾÒýÓõÄÎÄ×Ö -
     
    Wayne Shu, Mar 5, 2007
    #12
  13. arnuld

    arnuld Guest

    Re: Stroustrup: chapter 4

    > On Mar 5, 11:41 am, "Wayne Shu" <> wrote:


    > > fine but why:

    >
    > > char c; is a definition

    >
    > because it allocates storage for the variable c.
    >
    > > and

    >
    > > extern char c; is not


    it means:

    double sqrt(double);

    will not reserve any storage.


    > but this statement is an extern declaration ,and doesn't allocate
    > storage.


    > BTW, if the extern declaration has an initializer, it becomes a
    > definition.


    > e.g. extern char c = 'a'; // a definition.


    interesting
     
    arnuld, Mar 5, 2007
    #13
  14. arnuld

    benben Guest

    Re: Stroustrup: chapter 4

    > fine but why:
    >
    > char c; is a definition
    >
    > and
    >
    > extern char c; is not
    >
    > ?


    Because you need some way to declare a variable without defining it. The
    Standard tells you the right way to do it is to place an extern in the
    front.

    So
    char c; // definition
    extern char c; // declaration. used here, defined else where

    Just a rule, don't ask why.

    Ben
     
    benben, Mar 5, 2007
    #14
    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. arnuld

    Stroustrup chapter 3 - 3.6

    arnuld, Nov 5, 2006, in forum: C++
    Replies:
    4
    Views:
    426
    arnuld
    Nov 5, 2006
  2. arnuld
    Replies:
    14
    Views:
    1,057
    Alf P. Steinbach
    Nov 11, 2006
  3. arnuld
    Replies:
    0
    Views:
    448
    arnuld
    Mar 5, 2007
  4. arnuld
    Replies:
    0
    Views:
    531
    arnuld
    Mar 5, 2007
  5. arnuld
    Replies:
    4
    Views:
    556
    mlimber
    Mar 5, 2007
Loading...

Share This Page