473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Stroustrup: chapter 4

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
14 2286
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
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
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
On Mar 4, 10:59 am, "Kirit Sælensminde" <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
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
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
arnuld wrote:
>On Mar 4, 10:59 am, "Kirit Sælensminde"
<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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Oplec | last post by:
Hi, I am learning standard C++ as a hobby. The C++ Programming Language : Special Edition has been the principal source for my information. I read the entirety of the book and concluded that I...
11
by: Steven T. Hatton | last post by:
In TC++PL(SE) Stroustrup asserts there is a companion reference called _The Annotated C++ Language Standard_ by Koenig & Stroustrup. It doesn't show up on a google. What gives here? I know there...
1
by: Mike | last post by:
Hi, I'm auctioning the book "The C++ Programming Language" 3rd Ed. by Bjarne Stroustrup on ebay, details as follows or see eBay item number 250030480853. Softback. Condition : Good. Pub....
11
by: arnuld | last post by:
this is the code which runs without any trouble: ----------------------------------------------------- #include <iostream> #include <string> #include <vector> struct Entry { std::string...
14
by: arnuld | last post by:
Stroustrup starts chapter 6 with a programme for desk-calculator: here is a grammer for the langugae accepted by the calcualtor: program: END // END is end-of-input...
0
by: arnuld | last post by:
Stroustrup has a table in section 4.9 of declarations and definitions. he asks to write a similar table but in opposite sense: char ch; // declaration with definition he asks to do the...
0
by: arnuld | last post by:
this programme runs without any trouble. it took 45 minutes of typing. i posted it here so that people can save their precious time: // Stroustrup special edition // chapter 4 exercise 2 //...
14
by: arnuld | last post by:
there is no "compile-time error". after i enter input and hit ENTER i get a run-time error. here is the code: ---------- PROGRAMME -------------- /* Stroustrup, 5.9, exercise 11 STATEMENT:...
1
by: blangela | last post by:
Bjarne Stroustrup has a new text coming out called "Programming: Principles and Practice Using C++" (ISBN: 0321543726), due to be published in December of this year. Some of the features of this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.