Confusion on an Array based stack implementation.

C

Chris Mabee

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!
 
A

Artie Gold

Chris said:
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
 
K

Karthik Kumar

Chris said:
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.
 
C

Chris Mabee

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
 
J

Jonathan Mcdougall

Chris Mabee said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top