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;
};
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#.
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#.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With