Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does const denote here?

Tags:

c++

c#

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;
};
like image 391
softwarematter Avatar asked Dec 08 '22 01:12

softwarematter


2 Answers

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#.

like image 60
kyoryu Avatar answered Dec 09 '22 14:12

kyoryu


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#.

like image 39
James McNellis Avatar answered Dec 09 '22 13:12

James McNellis