473,700 Members | 2,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Confusion on an Array based stack implementation.

Hello all, and Merry Christmas,
I'm having a problem understanding an example of an array based
implementation of a stack in a textbook of mine. The code in question
is written below. The syntax is directly as in the book, except for
where I added the comments at the lines I wanted to refer to or to skip
sections of code.

template <class Element_Type>
class Stack
{
private:
// some variable declarations

Stack( const Stack & Value); // *1

public:

Stack(unsigned int Max_Size = 100);
const Stack & operator = ( const Stack & Value ); // *2

//destructor,push , and pop declarations
const Element_Type & Pop_And_Top();
const Element_Type & Top() const; // *3
//rest of declarations
};

The code at *1 and *2 is particularly troubling me. What I see at
*1 seems to be a call to the constructor using an unassigned stack
reference called Value. This makes no sense to me, so I'm assuming my
take on it is wrong. At *2 I see the same structure used to override
the assignment operator, but I'm really hazy on how this is working.
At *3 I don't know what the meaning of the extra 'const' at the end
of the line does. I was assuming that the functions were declared
'const Element_Type &' as references to an Element_Type, and were
constant because references can't be redefined. Again, feel free to
bust my bubble on this one. The extra 'const' through me for a loop.
The code for all of the functions is fairly straightforward . They
are all inline if it makes any difference. Thanks for any help.

Chris

BTW: This is my first post to this newsgroup so if it the format of the
message leaves something to be desired, let me know so my future posts
can be more effective. Thanks!

Jul 22 '05 #1
4 2618
Chris Mabee wrote:
Hello all, and Merry Christmas,
I'm having a problem understanding an example of an array based
implementation of a stack in a textbook of mine. The code in question
is written below. The syntax is directly as in the book, except for
where I added the comments at the lines I wanted to refer to or to skip
sections of code.

template <class Element_Type>
class Stack
{
private:
// some variable declarations

Stack( const Stack & Value); // *1

public:

Stack(unsigned int Max_Size = 100);
const Stack & operator = ( const Stack & Value ); // *2

//destructor,push , and pop declarations
const Element_Type & Pop_And_Top();
const Element_Type & Top() const; // *3
//rest of declarations
};

The code at *1 and *2 is particularly troubling me. What I see at
*1 seems to be a call to the constructor using an unassigned stack
reference called Value. This makes no sense to me, so I'm assuming my
take on it is wrong.
Indeed, your take *is* wrong. ;-(
What you're seeing at *1 is the *declaration* of a private copy
constructor (taking a `const' reference to a Stack object as an argument).

At *2 I see the same structure used to override
the assignment operator, but I'm really hazy on how this is working.
It's the *declaration* of an assignment operator for Stack objects.
At *3 I don't know what the meaning of the extra 'const' at the end
of the line does. I was assuming that the functions were declared
'const Element_Type &' as references to an Element_Type, and were
constant because references can't be redefined. Again, feel free to
bust my bubble on this one. The extra 'const' through me for a loop.
The code for all of the functions is fairly straightforward . They
are all inline if it makes any difference. Thanks for any help.
The `const' in question refers to the fact that the member function
`Top' does not change the `Stack' object on which it is called (you are,
however, correct that the returned value (the value at the top of the
stack) cannot be altered.
Chris

BTW: This is my first post to this newsgroup so if it the format of the
message leaves something to be desired, let me know so my future posts
can be more effective. Thanks!

Your posting is pretty clear. You *do*, however, really need to get a
good textbook for the C++ language. Several suggestions for this can be
found at http://www.accu.org, taking your previous programming
experience into account.

Welcome aboard, and good luck. Posting-wise, you're off to a good start.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 22 '05 #2
Thank you very much for the quick response! It was very insightful.
The book that I pulled the code from omitted the code for the
declarations of *1 and *2 but not for anything else... I have been
looking at purchasing the book by Bjarne Stroustrup, and it is on the
www.accu.org site so that boosted my confidence. Thanks again!

Chris

Jul 22 '05 #3
Chris Mabee wrote:
Hello all, and Merry Christmas,
I'm having a problem understanding an example of an array based
implementation of a stack in a textbook of mine. The code in question
is written below. The syntax is directly as in the book, except for
where I added the comments at the lines I wanted to refer to or to skip
sections of code.

template <class Element_Type>
class Stack
{
private:
// some variable declarations

Stack( const Stack & Value); // *1

public:

Stack(unsigned int Max_Size = 100);
const Stack & operator = ( const Stack & Value ); // *2

//destructor,push , and pop declarations
const Element_Type & Pop_And_Top(); const Element_Type & Top() const; // *3
//rest of declarations
};

The code at *1 and *2 is particularly troubling me. What I see at
*1 seems to be a call to the constructor using an unassigned stack
reference called Value.
It is called the 'copy constructor'. Also why do you think the reference
is unassigned ?

Stack<int> IntStack;

IntStack a;
IntStack b = a; // Invokes copy constructor. The reference in the
//copy constructor refers to 'a'.
//So yes - the reference is not assigned.
This makes no sense to me, so I'm assuming my
take on it is wrong. At *2 I see the same structure used to override
That is overloading the assigment operator.
the assignment operator, but I'm really hazy on how this is working.
IntStack a;
IntStack b;
b = a; // Invokes assignment operator.
At *3 I don't know what the meaning of the extra 'const' at the end
of the line does.
The const keyword says that the particular function cannot
modify the state of the class (cannot change any of its variables).

I was assuming that the functions were declared
'const Element_Type &' as references to an Element_Type, and were
constant because references can't be redefined. Again, feel free to
bust my bubble on this one. The extra 'const' through me for a loop.
The code for all of the functions is fairly straightforward . They
are all inline if it makes any difference. Thanks for any help.


When you see a function with a signature, that has got more than
one responsibility like this -

const Element_Type & Pop_And_Top();

in a given book,
I would suggest you try to get a better book to start learning
the programming language.
Jul 22 '05 #4
"Chris Mabee" <cm*****@yahoo. com> a écrit dans le message de news: > Hello
all, and Merry Christmas,
I'm having a problem understanding an example of an array based
implementation of a stack in a textbook of mine. The code in question
is written below. The syntax is directly as in the book, except for
where I added the comments at the lines I wanted to refer to or to skip
sections of code.

template <class Element_Type>
class Stack
{
private:
// some variable declarations

Stack( const Stack & Value); // *1
This is a copy-constructor. This is called with

Stack s1;
Stack s2(s1);

or

Stack s2 = s1;

Both do the same thing. Since that constructor is declared in the private
section of the class, that means the code does not compile, generating an
error like "Stack(cons t Stack&) is inaccessible". The author of the class
wanted to prevent Stack objects from being copy-constructed, but allowed
objects to be assigned (with the operator= declared in the public section).
That is quite unusual (either you allow copying or you don't), but it is
legal.
public:

Stack(unsigned int Max_Size = 100);
That's a constructor (a ctor, as you will learn to abbreviate). It is
called with either

Stack s;

or

Stack s(50);

because of the default argument.
const Stack & operator = ( const Stack & Value ); // *2
That's our operator=, which is used with

s1 = s2;

You may think

Stack s1 = s2;

also invokes it, but no. The = seen here is only another syntax for

Stack s1(s2);

which is a copy-ctor. It is used to make s1 look exactly like s2. It
usually involves clearing s1 and then copying all the elements from s2 to s1
so after the call, s1 and s2 look axactly the same.
//destructor,push , and pop declarations
Where's the dtor?
const Element_Type & Pop_And_Top();
That probably removes the top element from the stack and returns the new top
one. Returning it by reference is quite dangerous (what if there are no
more element on the stack?) If this function returns the element just
popped, that's illegal, since you are returning a reference to an element
already destroyed. (Who wrote that code?)
const Element_Type & Top() const; // *3
This probably returns the element on top of the stack, throwing an exception
if there are none. The const after the function means the *function* is
const. Conceptually, it means this function is not allowed to modify the
visible state of the object. That's a way to make sure you don't do
something stupid inside. It also allows const objects to call some
functions. For example, if you have

void f(const Stack &s)
{
s.Pop_And_Top() ; // illegal because s is const but not
// the function

s.Top(); // ok, since s is const and Top is also const
}

It allows the developper of the class to specify what can be called on a
const object. For example, member functions such as get_size() and clone()
will be const, since they do not modify the state of the object on which
they are called. Functions such as resize() or push() must not be const.
//rest of declarations
};

The code at *1 and *2 is particularly troubling me. What I see at
*1 seems to be a call to the constructor using an unassigned stack
reference called Value.
That sentence makes no sense.
This makes no sense to me, so I'm assuming my
take on it is wrong. At *2 I see the same structure used to override
the assignment operator, but I'm really hazy on how this is working.
Read about operator overloading.
At *3 I don't know what the meaning of the extra 'const' at the end
of the line does. I was assuming that the functions were declared
'const Element_Type &' as references to an Element_Type, and were
constant because references can't be redefined.
References cannot be reassigned (is that what you mean by "redefined" ?),
whether they are const or not. What const means here is that the object
returned is const, so you cannot modify it (or call non const member
functions on it).
Again, feel free to
bust my bubble on this one. The extra 'const' through me for a loop.
That means the member function is const. In reality, when you call a member
function, what happens is that that function receives a this pointer and
that pointer is applied implicitly.

class C
{
int a;

public:
void f()
{
a = 2;
}
};
int main()
{
C c;
c.f();
}

f() actually looks like this to the compiler:

void mangled_f(C *this)
{
this->a = 2;
}

If you try to call it from a const object

int main()
{
const C c;

c.f();
}

the compiler actually call

// c.f() becomes:
mangled_f(&c);

and you get the error "cannot convert from const C* to C*". If f() is
declared as const

class C
{
public:
void f() const;
};

the compiler translates that to

void mangled_f(const C* this);

and the call from a const object works.
The code for all of the functions is fairly straightforward . They
are all inline if it makes any difference. Thanks for any help.
It doesn't.
Chris

BTW: This is my first post to this newsgroup so if it the format of the
message leaves something to be desired, let me know so my future posts
can be more effective. Thanks!


Everything looks good to me! Have fun
Jonathan
Jul 22 '05 #5

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

Similar topics

4
2281
by: Erik Wikström | last post by:
In school (no I will not ask you to do my schoolwork for me) we talked about policy-based design and got an assignment where we got the a code- fragment from a stack-implementation. The idea with the code was that if, when its destructor was called, it still contained any elements it would run 'delete' on them if they were pointers and do nothing if they were not. Our assignment was to reimplement the stack using policies so that the...
24
3806
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
9
10688
by: dati_remo | last post by:
Hi, is it possible to find the dimension of an array using a pointer? main() { int a; f(a); return; }
1
403
by: chming | last post by:
It is ok to return a struct variable from a function. How about an array of any type from a function? Because the array is allocated in the stack, it is no meaning to return it, which ever type it is. This array is the memory of stack. But for a struct, it is allocated
21
3210
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a comma delimited list of values var jsData = new Array(); jsData = {lib: "#field...
10
3006
by: javuchi | last post by:
I just want to share some code with you, and have some comments and improvements if you want. This header file allocates and add and delete items of any kind of data from a very fast array: #include <stdlib.h> #ifndef __LIST_H__ #define __LIST_H__
14
20401
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is passed also. I understand that this way of passing the array is by value and if the prototype is declared as foo(int *), it is by reference in which case the value if modified in the function will get reflected in the main function as well. I dont...
35
2055
by: Andrew Fabbro | last post by:
I have a need to create an array that is larger than size_t - 1, which I believe is the largest guaranteed array. I though I'd found salvation in FAQ 6.14, but it appears I am not understanding something. The array is an array of pointers to structs. Here is an example using a small array: /* checks that malloc succeeded, main's return, etc. removed for brevity */
25
2348
by: biplab | last post by:
Hi all, I am using TC 3.0..there if I declare a integer array with dimension 162*219...an error msg saying that too long array is shown....what should I do to recover from this problem???
0
8642
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9069
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
8964
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
8918
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6558
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
5900
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
4400
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
4653
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3085
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.