473,624 Members | 2,612 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1442
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
5681
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 is mention of a problem here: http://www.research.att.com/~bs/bs_faq.html#ARM I guess this is the same work? This seems to mean there is a complete reference manual written by the two most qualified authors laying fallow. I find that disturbing....
7
1353
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> int main() { int tries = 1;
7
2343
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? which calls cause the compiler to to introduce a temporary variable? solution: this is the code ----------------------------------------------------------- #include <iostream> void f(char) {};
6
1754
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 year & the number of days in each month. write out that table. do this using: "a struct & an array of that struct" this is the solution i have created: #include <iostream>
14
4954
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 expr_list END expr_list: expression PRINT // PRINT is semicolon expression PRINT expr_list
14
2307
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
2436
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 STATEMENT: Read a sequence of words from the input. use "quit" as the word to terminate the input. Print the words in the order they were entered. don't print a word twice.modify the programme to sort the
5
1614
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 positive integers is almost never a good idea. Attempts to ensure that some values are positive by declaring variables unsigned will typically be defeated by the implicit conversion rules". I can't understand the two sentences.
6
2472
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 std::ifstream while passing a pointer to it, it automatically opens the file. 3) If I open a file using std::ostream, then I am confused whether it will open the file for writing or appending ?.
0
8685
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8631
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8341
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7174
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1489
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.