Stroustrup: chapter 4

A

arnuld

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
 
B

benben

arnuld said:
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?


Welcome

Ben
 
A

arnuld

arnuld wrote:

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.

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.
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

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
 
A

arnuld

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.

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
 
A

arnuld

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.
 
B

Bo Persson

arnuld said:
a File, is simply a File on my hard disk.



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
 
B

Bo Persson

arnuld said:
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
 
A

arnuld

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

?
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

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
 
W

Wayne Shu

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.
 
B

benben

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top