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