tags:

views:

206

answers:

2

What does const denote in the following C++ code? What is the equivalent of this in C#? I code in C# and I am trying to learn C++.

template <class T> class MaximumPQ { 
public:
virtual ~MaximumPQ () {}

virtual bool IsEmpty () const = 0;    

virtual void Push(const T&) = 0;

virtual void Pop () = 0;
};
+9  A: 

The first one informs the compiler that the method will not change any member variables of the object it is called on, and will also only make calls to other const methods.

Basically, it guarantees that the method is side-effect free.

The second one specifies that the object referred to by the passed reference will not be modified - that only const methods on it will be called.

There are no equivalent signatures in C#.

kyoryu
Well, it doesn't guarantee no side effects--just no side effects *on the object whose method is called*. That object can have, say, a `std::string *` as a field, and that `std::string` can be modified (via the dereferenced pointer) even if the method doing the modified is declared `const`.
Jonathan Grynspan
If calling a function has no side-effects, why call it? :)
GMan
@GMan: The return value doesn't count as a side effect.
James McNellis
@James: That...is something I totally decided to ignore for no reason.
GMan
@Jonathan Grynspan: You are absolutely correct. i didn't get into that simply to avoid complicating the answer further.
kyoryu
@kyoryu: Fair enough; I felt it was worth mentioning for future generations who bump into this question, lest they get the wrong impression (i.e. that they have to use `const_cast<>` everywhere or something... you know what I mean!)
Jonathan Grynspan
@Jonathan Grynspan: Not a problem - thank you for the educational addition :) Though, to be fair, const methods really *should* be side-effect free...
kyoryu
@kyoryu They should be *visible side effect* -free. Internal state is none of the calling code's business. :)
Jonathan Grynspan
@GMan: That error might have been the side-effect of alcohol. `:)`
sbi
sbi
@sbi: Ha, not at the time, but now... :D
GMan
+6  A: 

IsEmpty() is a const-qualified member function. It means that the this pointer is const-qualified, so it will have a type of const MaxPQ*. Code inside of IsEmpty() cannot call any member functions on this that are not themselves const-qualified, nor can it modify any data members that are not mutable.

To the best of my knowledge, there is nothing similar in C#.

James McNellis
"const MaxPQ*" - is it actually `const MaxPQ *const`, or is `this` non-const but non-modifiable as a special case?
Steve Jessop
@Steve: `this` is an rvalue so it can't be const-qualified (non-class type rvalues are never const- or volatile-qualified). It is because it is a non-class type rvalue that it cannot be modified.
James McNellis
@Steve: litb gave a good example a while ago. Imagine the parameter is `__this` with the type `cv-qualifiers class-name*`, then you have `#define this (__this + 0)`. Of course that's not necessarily what it is.
GMan
Thanks. And also, sorry, I probably should just have looked that up...
Steve Jessop