AngelScript
 
Loading...
Searching...
No Matches
Class methods

The class methods are implemented the same way as global functions, with the addition that the class method can access the class instance properties either directly or through the 'this' keyword in the case a local variable has the same name.

  // The class declaration
  class MyClass
  {
    // A class method
    void DoSomething()
    {
      // The class properties can be accessed directly
      a *= 2;

      // The declaration of a local variable may hide class properties
      int b = 42;

      // In this case the class property have to be accessed explicitly
      this.b = b;
    }

    // Class properties
    int a;
    int b;
  }

Const methods

Classes add a new type of function overload, i.e. const overload. When a class method is accessed through a read-only reference or handle, only methods that have been marked as constant can be invoked. When the reference or handle is writable, then both types can be invoked, with the preference being the non-const version in case both matches.

  class CMyClass
  {
    int method()       { a++; return a; } 
    int method() const {      return a; }
    int a;
  }
  void Function()
  {
     CMyClass o;
     const CMyClass @h = o;

     o.method(); // invokes the non-const version that increments the member a
     h.method(); // invokes the const version that doesn't increment the member a
  }