Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Confusion on an Array based stack implementation.

Reply
Thread Tools

Confusion on an Array based stack implementation.

 
 
Chris Mabee
Guest
Posts: n/a
 
      12-26-2004
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!

 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      12-26-2004
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
 
Reply With Quote
 
 
 
 
Karthik Kumar
Guest
Posts: n/a
 
      12-26-2004
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.
 
Reply With Quote
 
Chris Mabee
Guest
Posts: n/a
 
      12-26-2004
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

 
Reply With Quote
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      12-26-2004
"Chris Mabee" <> 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(const 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does std::stack::pop() not throw an exception if the stack is empty? Debajit Adhikary C++ 36 02-10-2011 08:54 PM
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
stack frame size on linux/solaris of a running application stack Surinder Singh C Programming 1 12-20-2007 01:16 PM
Re: x86 Stack Confusion Grumble C Programming 51 01-16-2006 03:29 AM
is RITE stack based or register based VM? rolo Ruby 5 04-05-2004 08:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57