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! 4 2599
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
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
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.
"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(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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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):
|
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;
}
|
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...
|
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...
|
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:
...
|
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...
|
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...
|
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???
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |