473,395 Members | 1,856 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,395 software developers and data experts.

Stroustrup section 2.5.1

here is the code from section 2.5.1 from Stroustrup (Special Edition):

namespace Stack {
struct Rep; // definition of stack layout is elsewhere
typedef Rep& stack;

stack create(); // make a new stack
void destroy(stack s); // delete s

void push(stack s, char c); // push c onto s
char pop(stack s);
}

Later Stroustrup says: "the declaration /struct Rep;/ says that /Rep/
is the name of a type but it leaves the type to be defined later.

doesn't it say that /Rep/ is of type /struct/ like /x/ is of type /int/
in /int x/ (hence /Rep/ is not a type but just a defined variable like
/x/ ?

Oct 31 '06 #1
3 1426
hey, sorry, i read "Eckel's book" & came to know that /struct/ is used
to define new types. Therefore Stroustrup must have used /struct Rep/
as a declaration as he puts. anyway, i got one more problem:

in the section 2.5.1 he defines Stack /namespace/ like this:

----------------------------------------------------
namespace Stack {
cont int max_size = 200;

struct Rep {
char v[max_size];
int top;
};

const int max=16;

Rep stcks[max];
bool used[max];

typedef Rep& stack;
}

// defines Stack::push here
// defines Stack::pop

Stack::stack Stack::create()
{
// pick an unused Rep, mark it used, initialise it
// and return a reference to it
}

// defines Stack::destroy here
--------------------------------------------------------------

my trouble is: /Stack::stack Stack::create/. what work this function
is doing?

Oct 31 '06 #2
* arnuld:
hey, sorry, i read "Eckel's book" & came to know that /struct/ is used
to define new types. Therefore Stroustrup must have used /struct Rep/
as a declaration as he puts. anyway, i got one more problem:

in the section 2.5.1 he defines Stack /namespace/ like this:

----------------------------------------------------
namespace Stack {
cont int max_size = 200;

struct Rep {
char v[max_size];
int top;
};

const int max=16;

Rep stcks[max];
bool used[max];

typedef Rep& stack;
}

// defines Stack::push here
// defines Stack::pop

Stack::stack Stack::create()
{
// pick an unused Rep, mark it used, initialise it
// and return a reference to it
}

// defines Stack::destroy here
--------------------------------------------------------------

my trouble is: /Stack::stack Stack::create/. what work this function
is doing?
Like the comment says (and that's all I have to go on, not having the
Special Edition but just some old ones):

1. Find a currently not used element of the 'stack' array; this
element will be used to represent a new stack.

2. Initialize that element.

3. Return a reference to it (note: 'Stack::stack' is defined as a
reference type).

From the client code's view 'Stack::create' creates a new stack.

There is a problem in what 'Stack::create' should do should it fail to
find an unused element of the 'stack' array. One strategy could then be
to call 'std::terminate'. And another, to throw an exception.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 31 '06 #3
Alf P. Steinbach wrote:

Like the comment says (and that's all I have to go on, not having the
Special Edition but just some old ones):

If you have the 3rd edition, it's in the same section. Starts on page
30 in my printing. As far as I know, the Special is the 3rd with a
hardcover and (I think) some extra appendices.


Brian
Oct 31 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
7
by: arnuld | last post by:
i am trying to understand the mentioned programme, i modified it a little so that i could understand it better. here is the code & output: // Stroustrup - Tour of C++ #include <iostream> ...
7
by: arnuld | last post by:
problem: define functions F(char), g(char&) & h(const char&). call them with arguments 'a', 49, 3300, c, uc & sc where c is a char, uc is unsigned char & sc is signed char. whihc calls are legal?...
6
by: arnuld | last post by:
i do not have any problem here. i solved the problem but i wanted to know the views of you. please look at it from a newbie's perspective: problem: define a table with names of months of the...
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...
14
by: arnuld | last post by:
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&)
27
by: arnuld | last post by:
it works fine without any trouble. i want to have advice on improving the code from any angle like readability, maintenance etc: ---------- PROGRAMME ------------ /* Stroustrup, 5.9, exercise 11...
5
by: Wayne Shu | last post by:
Now I'm reading Stroustrup's The C++ Programming Language(Special Edition). In section 4.4 Integer Types, he has wrote that "Using an unsigned instead of an int to gain one more bit to represent...
6
by: arnuld | last post by:
This one works to seem fine. Can I make this program better ? 1) the use of get(ch) function was inspired from Stroustrup 21.5.1, page number 638. 2) I see, when you create an object of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.