Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Stroustrup: chapter 4

Reply
Thread Tools

Stroustrup: chapter 4

 
 
arnuld
Guest
Posts: n/a
 
      03-04-2007
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

 
Reply With Quote
 
 
 
 
benben
Guest
Posts: n/a
 
      03-04-2007
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
 
Reply With Quote
 
 
 
 
arnuld
Guest
Posts: n/a
 
      03-04-2007
> 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.

 
Reply With Quote
 
=?iso-8859-1?q?Kirit_S=E6lensminde?=
Guest
Posts: n/a
 
      03-04-2007
On Mar 4, 11:40 am, "arnuld" <(E-Mail Removed)> 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

 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      03-04-2007
> On Mar 4, 10:59 am, "Kirit Sælensminde" <(E-Mail Removed)> 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

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      03-04-2007

> 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.
 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      03-04-2007
> On Mar 4, 2:27 pm, John Harrison <(E-Mail Removed)> 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.

 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      03-04-2007
arnuld wrote:
>> On Mar 4, 10:59 am, "Kirit Sælensminde"
>> <(E-Mail Removed)> 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


 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      03-04-2007
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


 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      03-05-2007
> 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

?




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
70-315: Lab of Chapter 5 2obvious MCSD 0 10-19-2005 12:12 AM
70-315: Lab of Chapter 5 =?Utf-8?B?SmFja0JpZ01hYw==?= MCSD 0 02-23-2005 05:35 PM
70-305, Chapter 5 (ADO): Login failed Irene MCSD 9 02-13-2004 03:50 AM
Cisco Networking Simplified - book review and a sample chapter Michael Cisco 0 11-19-2003 11:09 PM
107-188 class, Chapter 1 Step 5 Hallema Morgan Microsoft Certification 0 08-26-2003 11:26 PM



Advertisments