Abstract Functions

Class member functions (including class operator functions) can be made abstract by using the declaration as follows: int func() = 0;

  • Abstract functions cannot be executed because they do not contain code.
  • An abstract class is a class that either defines or inherits at least one abstract function.
  • Objects of an abstract class can be created, but data members whose type is an abstract class cannot be declared.
  • Abstract types cannot be used as value parameter types or as function return types.
  • shared_ptr and reference function arguments to an abstract class can be declared.
  • A class derived from an abstract class must define all functions that are still abstract as non-abstract.
  • You cannot make the constructor or destructor abstract.
  • Abstract function must not be static.
Einschränkung: Note that due to the current limitations of the parser, not all illegal lines of code are detected during parsing (i.e. they do not lead to a syntax error even though they are forbidden) and instead trigger a CTRL runtime exception. This applies e.g. to the use of the abstract class as a data type in member functions of the abstract class itself.
// WRONG - DO NOT USE, but does not produce a syntax error:
class Abstract
{
  public void abstractFunc() = 0;
  public Abstract someFunc() { return Abstract(); }
};


// CORRECT usage:
class ILogger
{
  public void write(const string &text) = 0;
};

class MyLogger : ILogger
{
  public void write(const string &text)
  {
    DebugN(text);
  }
};

class MyLogger2 : ILogger
{
  public void write(const string &text)
  {
    DebugN("Another logger: " + text);
  }
};

// "Strategy pattern" / IoC (inversion of control) sample
main(mapping event)
{
  bool use2 = false; //Just change a single line of code (true to false) to use
  shared_ptr<ILogger> f;

  if (use2)
   f = new MyLogger2();
  else
   f = new MyLogger();

  /* If you use this approach/pattern, the advantage is that only one place in the code needs to be adjusted when the logger is changed, 
     see if (use2) above - the f.write() already refers to the correct method */
  f.write("This is a test");
  f.write("This is a test");
  f.write("This is a test");
  f.write("This is a test");
  f.write("This is a test");
}