0
//Array-based Class ‘LinearList’
template <class T>
class LinearList 
{
public: 
  LinearList(int MaxListSize = 10);
  ~LinearList() { delete [] element; }

  bool isEmpty() const { return length == 0; } //1
  int Length() const { return length; } //2
  bool Find(int k, T& x) const; //3
  int Search(const T& x) const; //4
  LinearList<T>& Delete(int k, T& x);
  LinearList<T>& Insert(int k, const T& x); //5
  void Output(ostream& out) const; //6

private: 
  int length;
  int MaxSize;
  T *element;
};
Sean
  • 60,939
  • 11
  • 97
  • 136
Bijon Guha
  • 51
  • 2
  • 9
  • 1
    Your question and code is unintelligible you need to edit your question and include your question and what your understanding is – EdChum Jun 03 '16 at 09:50
  • The topic describes the question quite fine, I think – IceFire Jun 03 '16 at 09:58
  • Tried to add an answer, but the Q got closed, hence a comment: The declaration is quite like appending the `const` qualifier to the (implicit) `this` declaration's type: a `bool List::Destroy()` method would have an implicit `List *this` parameter, whilst `bool List::isEmpty() const` will get `const List *this`. – CiaPan Jun 03 '16 at 10:04

2 Answers2

2

These lines simply mean that if you instantiate the class, you cannot use these methods unless your class is a const instance or - more precisely - the reference/pointer/access to your object is const. So, let's say, you have some function where such a list is passed:

void readSomethingFromList(const LinearList<int>& list)
{
    list.Insert(10, 20); // not working, compile-time error
    int n = list.Length(); // this is fine
}

So, const marks a method as readonly and if the object is not readonly, then it cannot be used and will not compile. This assures that a function like readSomethingFromList will really not change the object in any way. This makes your code less prone to errors and all in all more stable. So, whenever you have methods that will not change the object, you should mark them const.

Since this is basic knowledge, you should probably read a book about C++ thoroughly. Otherwise, you will also miss other important language concepts of C++ and might even apply them in a wrong way, which can be fatal. As this is so basic, you probably also get all those downvotes.

IceFire
  • 4,016
  • 2
  • 31
  • 51
  • You don't have to have a 'const instance', the rule applies also to 'const references' – the object itself may be modifiable, it's just considered `const` in the current method. – CiaPan Jun 03 '16 at 10:05
  • Yes, this is more precise. My example has no const instance, either, so this is hopefully clear – IceFire Jun 03 '16 at 10:06
  • okay thanks @IceFire. i will surely refer a book now. – Bijon Guha Jun 03 '16 at 12:55
2

A const at the end of a method declaration (ie void Foo() const) means that the method does not alter the state of the object.

When a parameter is flagged as const it means the method cannot change the parameter. For example, void DoSomething(const T &t) declares that DoSomething can only call methods declared as const on the object.

Sean
  • 60,939
  • 11
  • 97
  • 136