By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,949 Members | 1,504 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,949 IT Pros & Developers. It's quick & easy.

Stroustrup: chapter 4

P: n/a
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

Mar 4 '07 #1
Share this Question
Share on Google+
14 Replies


P: n/a
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
Mar 4 '07 #2

P: n/a
On Mar 4, 10:17 am, benben <benhonghatgmaildotcom@nospamwrote:
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.

Mar 4 '07 #3

P: n/a
On Mar 4, 11:40 am, "arnuld" <geek.arn...@gmail.comwrote:
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

Mar 4 '07 #4

P: n/a
On Mar 4, 10:59 am, "Kirit S鎙ensminde" <kirit.saelensmi...@gmail.comwrote:
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

Mar 4 '07 #5

P: n/a
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.
Mar 4 '07 #6

P: n/a
On Mar 4, 2:27 pm, John Harrison <john_androni...@hotmail.comwrote:
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.

Mar 4 '07 #7

P: n/a
arnuld wrote:
>On Mar 4, 10:59 am, "Kirit S鎙ensminde"
<kirit.saelensmi...@gmail.comwrote:
>>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
Mar 4 '07 #8

P: n/a
arnuld wrote:
>On Mar 4, 10:17 am, benben <benhonghatgmaildotcom@nospamwrote:
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
Mar 4 '07 #9

P: n/a
arnuld wrote:
>On Mar 4, 10:17 am, benben <benhonghatgmaildotcom@nospamwrote:
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 would not want to comment on the way I consider how the person in
question manages things. But if I were to, the example you have given
would not be my evidence either.
>
>>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 ?
[snip]
>
i want to know WHY the 3 declarations i posted are "declarations with
defintions"?
Okay. Here we go.

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.
>
>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.
See above.

Ben
Mar 5 '07 #10

P: n/a
On Mar 5, 8:28 am, benben <benhonghatgmaildotcom@nospamwrote:

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

?


Mar 5 '07 #11

P: n/a
On Mar 5, 1:19 pm, "arnuld" <geek.arn...@gmail.comwrote:
On Mar 5, 8:28 am, benben <benhonghatgmaildotcom@nospamwrote:
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

Mar 5 '07 #12

P: n/a
On 3月5日, 下午2时19分, "arnuld" <geek.arn...@gmail..comwrote:
On Mar 5, 8:28 am, benben <benhonghatgmaildotcom@nospamwrote:
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.
>
?- 隐藏被引用文字 -

- 显示引用的文字 -

Mar 5 '07 #13

P: n/a
On Mar 5, 11:41 am, "Wayne Shu" <Wayne...@gmail.comwrote:

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

Mar 5 '07 #14

P: n/a
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
Mar 5 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.