Welcome! Log In Create A New Profile

Advanced

Assignment2 question 10

Posted by whatever 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Assignment2 question 10
May 25, 2009 09:40AM
Hi there,

Does anybody have an explanation why the following code does not want to compile

Language: C++
template<class Type> void stackType<Type>::reverseStack(const stackType<Type>& otherStack){   //Preconditions : array size must be the same assert (maxStackSize == otherStack.maxStackSize );   while ( ! isEmptyStack() ){ Type temp = top(); otherStack.push( temp ); pop(); }     }

I get the following compiler error
...myStack.h passing `const stackType<int>' as `this' argument of `void stackType<Type>::push(const Type& ) [with Type = int]' discards qualifiers
Re: Assignment2 question 10
May 25, 2009 10:06AM
Found the answer

It is simple in concept, variables declared with ‘const’ become constants and cannot be altered by the program
avatar Re: Assignment2 question 10
May 25, 2009 05:58PM
Your answer is technically correct but there is a subtlty that you've missed.

The parameter variable - otherStack - is declared as a constant and is thus treated like a constant for the duration of the function. What the compiler was complaining about was that you called the member function push(const Type &winking smiley on this object. This is not allowed.

If you wish to have constant objects but still be able to use their member functions (or at least some of them), you need to give a guarantee that you won't alter any data members inside that member function. You do this by affixing the keyword const to the end of the function header. This must also be in the definition if you put it in a separate source file. If you've got the const keyword there, the compiler will enforce your guarantee and prevent you from altering data members in that function.

eg
Language: C++
class myClass{ public: int getValue() const; void setValue(int newValue); private: int value; };   int myClass::getValue() const{ // must include the const here too return value; // this is allowed because you';re not changing the value. }   // data are allowed to be changed in the next function because it';s not marked as a constant function. If it were, a compiler error would be generated on the line that changes the data member. void myClass::setValue(int newValue) { value = newValue; }     ////////   Usage   ///////   int someFunction(const myClass &obj){ obj.setValue(10); // not allowed because obj is declared as a constant in the parameter list return obj.getValue(); // this is allowed because getValue() has been declared as a constant function and guarantees that the data won';t be changed }

I hope this makes things clearer.
Sorry, only registered users may post in this forum.

Click here to login