4

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;
};
softwarematter
  • 28,015
  • 64
  • 169
  • 263
  • 4
    Please get a [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) so you can properly learn C++. – GManNickG Sep 19 '10 at 21:37
  • can u suggest one for person familiar with C#? – softwarematter Sep 20 '10 at 07:56
  • Familiarity with C# is irrelevant, they are different languages for a reason. I recommend a beginner book since you are, in fact, a beginner. C# isn't going to help you, pretend you don't know it. – GManNickG Sep 20 '10 at 09:21

2 Answers2

9

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
  • 12,848
  • 2
  • 29
  • 33
  • 6
    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 Sep 19 '10 at 21:31
  • If calling a function has no side-effects, why call it? :) – GManNickG Sep 19 '10 at 21:37
  • 1
    @GMan: The return value doesn't count as a side effect. – James McNellis Sep 19 '10 at 21:38
  • 2
    @James: That...is something I totally decided to ignore for no reason. – GManNickG Sep 19 '10 at 21:41
  • @Jonathan Grynspan: You are absolutely correct. i didn't get into that simply to avoid complicating the answer further. – kyoryu Sep 19 '10 at 22:12
  • @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 Sep 20 '10 at 02:51
  • @Jonathan Grynspan: Not a problem - thank you for the educational addition :) Though, to be fair, const methods really *should* be side-effect free... – kyoryu Sep 20 '10 at 04:01
  • @kyoryu They should be *visible side effect* -free. Internal state is none of the calling code's business. :) – Jonathan Grynspan Sep 20 '10 at 04:15
  • @GMan: That error might have been the side-effect of alcohol. `:)` – sbi Sep 20 '10 at 05:57
  • 1
    @Jonathat & @kyoryu: Actually, side-effects are also things like changing of other objects (like those passed into the function per reference or pointer) outputting to disk or to the user, whatever... And a `const` member function might still do all these things. Also, it might even change member data, be it `mutable` data, or non-`mutable` data (through casting away the `const`) The _only_ thing that `const` says is that it is the intent of the function to leave the object `const`. Side-effects are not involved at all. – sbi Sep 20 '10 at 06:01
  • @sbi: Ha, not at the time, but now... :D – GManNickG Sep 20 '10 at 06:34
6

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
  • 348,265
  • 75
  • 913
  • 977
  • "const MaxPQ*" - is it actually `const MaxPQ *const`, or is `this` non-const but non-modifiable as a special case? – Steve Jessop Sep 19 '10 at 21:43
  • 1
    @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 Sep 19 '10 at 21:47
  • @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. – GManNickG Sep 19 '10 at 21:51