Qore Programming Language Reference Manual 1.19.3
Loading...
Searching...
No Matches
Classes

Class Overview

Classes define types of Qore objects. Classes can define members and methods, which are attributes of the class and functions that operate only on the objects of that class, respectively. Furthermore access to class members and methods can be restricted with the private or private:internal access modifiers, and classes can be subclassed to support polymorphism.

A class can have any valid identifier name except "auto" but may not have the same name as a child namespace of the namespace parent object.

Note
Each Qore type has a "pseudo-class" associated with it (the default is <value>); methods from the data type's "pseudo-class" can be run on any value of that type; see <value> and "Pseudo Class for Type" headings in Basic Data Types for more information.
In-Line Class Declaration Syntax
[public] [final] class [namespace_path::...]class_identifier] [inherits [private[:internal|hierarchy]|public] [namespace_path::...]parent_class_identifier[, ...]] {
    [[private[:internal|hierarchy]] [transient] member_name[, ...];]

    [private[:internal|hierarchy]|public {
        [[transient] [member_type] member_name [= initialization_expression];]
        [static [static_member_type] static_member_name [= initialization_expression];]
        [const constant_name = initialization_expression;]
    }]

    [[private[:internal|hierarchy]|public] [deprecated] constructor([[param_type] param_name [= default_initialization_expression], ...]) [: parent_class_name(args...), ...] {
    }]
    [copy(object arg_name) {
    }]
    [destructor() {
    }]
    [[static|final] [synchronized] [private[:internal|hierarchy]|public] [deprecated] [return_type] method_name([[param_type] param_name [= default_initialization_expression], ...]) {
    }]
    [[abstract] [private[:internal|hierarchy]|public] [return_type] method_name([[param_type] param_name [= default_initialization_expression], ...]);]
    ...
}
Note
To define a class interface, define a class with abstract methods.

When defining a user module, class declarations can be preceded by public, which means that the class will be available (imported) in the Program object importing the module. When a class is declared public outside of a user module, it means that the class can be inherited in any child Program objects created in the same scope. See public for more information. Note that classes can also be imported singly by using the Program::importClass() method.

In-Line Class Declaration Example
class MyClass inherits MyBaseClass {
public {
string pub_attr = "hello";
const pub_const = "foo";
static pub_static_var = "bar";
}
constructor(string arg) : MyBaseClass(arg + 1) {
}
softstring myMethod(softint i) {
return i + 2;
}
}
Note
No semicolon (";") is required to terminate a class declaration (in fact using a semicolon would raise a parse exception).
Out-Of-Line Class Declaration Syntax
Alternatively class declarations and method declarations can be defined out of line as follows:
[public] class [namespace_path::...]class_identifier [inherits [private[:internal|hierarchy]|public] [namespace_path::...]parent_class_identifier[, ...]];

[[private[:internal|hierarchy]|public] [deprecated] [namespace_path::...]class_identifier::constructor([[param_type] param_name [= default_initialization_expression], ...]) [: parent_class_name(args...), ...] {
}]

[[static|final] [synchronized] [private[:internal|hierarchy]|public] [deprecated] [return_type] [namespace_path::...]class_identifier::method_name([[param_type] param_name [= default_initialization_expression], ...]) {
}] [abstract [private[:internal|hierarchy]|public] [return_type] [namespace_path::...]class_identifier::method_name([[param_type] param_name [= default_initialization_expression], ...]);
As with inline class definitions, when defining a user module, class declarations can be preceded by public, which means that the class will be available (imported) in the Program object importing the module. When a class is declared public outside of a user module, it means that the class can be inherited in any child Program objects created in the same scope. See public for more information. Note that classes can also be imported singly by using the Program::importClass() method.
Out-Of-Line Class Declaration Example
class MyNamespace::MyClass inherits MyBaseClass;
MyNamespace::MyClass::constructor(string arg = "temp") : MyBaseClass(arg + ".txt") {
}
softstring MyNamespace::MyClass::myMethod(softint i) {
return i + 2;
}
Note
Because method definitions are allowed out-of-line, this means that builtin classes may be extended with new user methods, however user constructor, destructor, and copy methods cannot be added to builtin classes; to customize the behavior of these methods for builtin classes, subclass the class instead.

When parse option %allow-bare-refs or %new-style is enabled, no "$" or "$." prefixes can be used with variable or method or member names. Class members, class constants, and static class variables can only be declared in an in-line class definition (the first example above). If a class has at least one public member declared (or inherits a class with at least one public member declared), then only those members declared as public can be accessed from outside the class, and from within the class only members explicitly declared can be accessed as well (unless the class also defines a memberGate() method). In this way typographical errors in member names can be caught (at parse time if types are declared).

In a class hierarchy, base class constructor methods can be explicitly specified using a special syntax unique to subclass constructor methods. Please see Class Inheritance for more information.

It's possible to write purely object-oriented scripts/programs in Qore by defining an application class and using the "-x" or "--exec-class" command-line arguments to tell Qore to instantiate the class instead of doing normal top-level execution (in fact, the "--exec-class" argument disallows the use of top-level statements entirely). For more information, please see qore Executable Command-Line Processing and Parse Directives.

Final Classes

Classes declared final cannot be subclassed, and, by extension, no methods of a final class can be reimplemented as well.

Currently the Qore standard class library (delivered with Qore) has no final classes.

Class Methods

Public Methods

All class methods are public by default, but methods can also be explicitly declared with the public access modifier as well. Public methods have no class protection and can be called from any code context.

Private Methods

Methods declared with the private access modifier can only be called by other member methods of the same class or in the same class hierarchy. Any attempt to call these methods from outside the class hierarchy will result in a run-time exception. This access modifier, when used without any modifiers, is equivalent to private:hierarchy and is functionally identical to C++'s protected access modifier.

Methods declared as private:internal are not available outside the class, even in classes in the same class hierarchy, providing strong encapsulation of the declared method. This can be used to ensure that the declared method can be changed in later versions of the class without affecting API compatibility.

Methods declared as private:hierarchy are equivalent to those declared private and are only private from outside the class hierarchy.

Synchronized Methods

Methods declared with the synchronized keyword will only run in one thread at a time.

synchronized normal class methods share a reentrant lock associated with the object, while synchronized static class methods share a reentrant lock associated with the class itself.

For more information, see the synchronized keyword.

Static Methods

Methods declared with the static keyword are like regular functions that are attached to the class. These methods are not associated with a particular object's state and therefore are not allowed to refer to object members or call non-static methods. Also, no reference to the special self variable is allowed within static methods.

Static methods may be declared private, private:internal, or public or synchronized like non-static methods; static methods can also access private members of a class (through an object of the class for non-static members). Static methods may not be also abstract.

Static method calls take a special syntax as documented in Static Method Calls .

Abstract Methods

Methods declared with the abstract keyword define methods interfaces that must be defined in child classes for the class to be instantiated. Methods defining the declared interfaces in child classes must define exactly the same parameters in order for the abstract method to match, but the return type of the concrete method in the child class has to simply be compatible with the return type of the abstract method in the parent class.

Classes with undefined abstract methods cannot be instantiated. Abstract methods cannot have a method body; an abstract method's declaration must be terminated with a semicolon (";"). Furthermore, abstract methods cannot be also static.

The abstract keyword can only be used in abstract method declaration; the use of this keyword with a method declaration with a method body will cause a parse exception to be thrown.
\N

Note
Ellipses ("...") may be provided as the only or last parameter, indicating the the method takes a variable number of arguments. This is most important with abstract class methods and any concrete methods in child classes implementing the abstract method, as the ellipses must match, or the method will not be overridden.

Constructors, Destructors, and Other Special Methods

All class methods are optional, but some methods have a special meaning.

Special Methods

Name Description
constructor([params...]) {} Called when objects are created when instantiated by a variable declaration with a class type and constructor arguments or explicitly with the new operator. User code may not explicitly call constructor() methods directly. In a class tree, constructor() methods are called for base classes first in left-to-right, depth-first declaration order.

constructor() methods may be overloaded and also private constructors may be defined. Constructors declared private can only be called from within the class hierarchy, and those declared private:internal can only be called within the class itself.
copy() {} When a user explicitly calls a copy method, Qore will generate a new object with references to the same members as the source object. Then, if there are any base classes, base class copy() methods are called in the same order as the constructor() methods. If a copy() method is defined, it will be run in the new object with a reference to the old object passed as the first argument. Any other arguments passed to the copy() method are ignored.

copy() methods cannot be overloaded and cannot be private.
destructor() {} Called when objects are implicitly collected (see Garbage Collection and Resource Management) or are explicitly deleted. User code may not explicitly call destructor() methods. In a class tree, destructor() methods are called for base classes in the opposite order in which the constructors are called.

destructor() methods cannot be overloaded and cannot be private.
any memberGate(string $member_param_name) {} If this method is implemented in the class, it is called when read access is attempted to private member or members that do not exist in the current object; the return value of this method is returned as the value of the member.

memberGate() methods cannot be overloaded and are not inherited by subclasses.
any methodGate(string $method_param_name, ...) {} If this method is implemented in the class, it is called when methods are called on the object that do not exist in the current object and the return value of this method is returned as the value of the method call.

methodGate() methods cannot be overloaded and are not inherited by subclasses.
memberNotification(string $member_param_name) {} If this method is implemented in the class, it is called when an object member is updated outside the class with the member name as the argument. Note that this method is called after the member has been updated and without locking; the call is not atomic respective to other threads that also may update the same member simultaneously.

memberNotification() methods cannot be overloaded and are not inherited by subclasses.

Final Methods

Methods declared final cannot be reimplemented in a subclass. Methods can be declared final to prevent a subclass from altering the behavior of a method that is critical to the class's functionality.

None of the following special methods can be declared final:

  • constructor(): declare the class final instead
  • destructor(): declare the class final instead
  • copy(): declare the class final instead
  • memberGate(): these methods are not inherited so they may not be declared final
  • memberNotification(): these methods are not inherited so they may not be declared final
  • methodGate(): these methods are not inherited so they may not be declared final

Ellipses For Variable Arguments

Ellipses ("...") may be provided as the only or last parameter for any method, indicating the the method takes a variable number of arguments.

This is most important with abstract class methods.

Class Constants

Class constants, like non-class constants, allow programmers to refer to values with Qore identifiers rather than using the value or the expression that generates the value.

See Class Overview for a description of the syntax required to declare a class constant.

Like other attributes of classes, class constants may be declared private, private:internal, or public. The following are examples of class constant definitions:

class Test {
public {
const Version = "1.0";
}
private {
const Limit = 100;
}
private:internal {
const Other = "string";
}
}
Note
Class constants cannot have the same names as static class variables.

Static Class Variables

Static class variables are like global variables that belong to a class. They are not associated with any particular object.

See Class Overview for a description of the syntax required to declare a static class variable.

Like other attributes of classes, static class variables may be declared private, private:internal, or public. The following are examples of static class variable definitions:

class Test {
public {
static string lastFile = "none";
}
private {
static int numProcessed = 0;
}
private:internal {
static string other = "string";
}
}
Note
Static class variables cannot have the same name as class constants.

Class Members

Public Member Declarations

If a class has at least one public member declared (or inherits a class with at least one public member declared), then only those members declared as public can be accessed from outside the class, and from within the class only members explicitly declared can be accessed as well (unless the class also defines a memberGate() method). In this way typographical errors in member names can be caught (at parse time if types are declared).

Private:Internal Members

Members declared private can only be accessed within the class hierarchy; trying to access private members form outside the class hierarchy will result in either a parse or runtime exception, depending on when the illegal access is caught. This access modifier, when used without any modifiers, is equivalent to private:hierarchy and is functionally identical to C++'s protected access modifier.

Members declared as private:internal are not available outside the class, even in classes in the same class hierarchy, providing strong encapsulation of the declared member. This can be used to ensure that the declared member can be changed in later versions of the class without affecting API compatibility.

Members declared as private:hierarchy are equivalent to those declared private and are only private from outside the class hierarchy.

Transient Members

Members declared with the transient keyword will not be processed with object serialization and will get their default value (if any) when deserialized.

Class Member References

When defining a class when using the old style syntax, members of instantiated objects are referred to with a special syntax as follows:
$.member_name

Furthermore, the automatic variable self is instantiated in every non-static method, representing the current object (similar to this in C++ or Java). Therefore if you need to access hash members which are not valid Qore identifiers, then enclose the member name in double quotes after the dot operator as follows:

self."&member-name"

memberGate() Method

If the class implements a memberGate() method, then whenever a non-existent member of the class is accessed (read), this method will be called with the name of the member as the sole argument, so that the class can create the member (or react in some other way) on demand. This method is also called when methods of the same class try to access (read) non-existent methods, but is not called from within the memberGate() method itself.

Note
memberGate() methods are not inherited; they must be explicitly implemented in each class

memberNotification() Methods

To monitor writes to the object, the class can implement a memberNotification() method, which is called whenever an object member is modified from outside class member code. In this case, the memberNotification() method is called with the name of the member that was updated so that an object can automatically react to changes to its members (writes to members) from outside the class. This method is not called when members are updated from within class member code.

Note
memberNotification() methods are not inherited; they must be explicitly implemented in each class

Member Initialization

Members that have intialization expressions in the class definition are initialized before the constructor is executed (but after any base class constructors have run). An exception raised in a member initialization expression will cause the constructor to fail and for the object to be deleted immediately.

Note
The automatic argv local variable is instantiated as usual in all class methods where there are more arguments than variables declared in the method declaration.

Object Method Calls

In-Class Method Call Syntax
Within a class method definition, calls to methods in the same class hierarchy (of the current class or a base class) can be made as follows:
[[namespace_path::...]parent_class_name::]$.method_name([args, ...])
In-Class Method Call Example
# to call a specific method in a base class
Thread::Mutex::lock();
# to call lock() in the current (or lower base) class
lock();
In-Class Method Calls with allow-bare-refs
When the %allow-bare-refs or %new-style parse directive is set, then object methods are called without the "$." prefix as in the following example:
# to call a specific method in a base class
Thread::Mutex::lock();
# to call lock() in the current (or lower base) class
lock();

Calls to object methods can be made outside the class by using the above syntax as well. If the object's class is not known at parse time, then the call is resolved at run-time, and if a call is attempted to a private function outside the defining class, then a run-time METHOD-IS-PRIVATE (if the method is private) or BASE-CLASS-IS-PRIVATE (if the method resolves to a privately-inherited base class) exception is raised.

methodGate() methods
If the class implements a methodGate() method, then whenever a non-existent method of the class is called, the methodGate() method will be called with the name of the member as the first argument (prepended to the other arguments to the non-existant method), so that the class simulate or redirect the method call. This method is also called when methods of the same class try to call non-existent methods, but is not called from within the methodGate() method itself.
Note
methodGate() methods are not inherited; they must be explicitly implemented in each class

Class Inheritance

Class inheritance is a powerful concept for easily extending and reusing object-oriented code, but is also subject to some limitations. This section explains how class inheritance works in Qore.

Classes inherit the methods of a parent class by using the inherits keyword as specified above. Multiple inheritance is supported; a single Qore class can inherit one or more classes. When a class is inherited by another class, it is called a base class or parent class. Private inheritance is specified by including the private access modifier before the inherited class's name. When a class is privately inherited, it means that the inherited class's public declarations (members, constants, methods, etc) are treated as private in the context of accesses outside the class hierarchy.

To limit access of a base class to only the directly inheriting class, use the private:internal access modifier. Classes inherited with private:internal are not accessible to the hierarchy in general but rather only to the directly inheriting class.

Inheritance is public by default, to inherit a class privately, such that the inherted class is accessible within all child classes in the hierarchy, use the private access modifier before the class name or class path to inherit as follows:

class ChildClass inherits private ParentClass {
}

To inherit a class where the inherited class is only available to the inheriting class but not to child classes, use the private:internal access modifier before the class name or class path as follows:

class ChildClass inherits private:internal ParentClass {
}

It is not legal to directly inherit the same class directly more than once; that is; it is not legal to list the same class more than once after the inherits keyword. However, it is possible that a base class could appear more than once in the inheritance tree if that class is inherited separately by two or more classes in the tree.

In this case, the base class will actually only be inherited once in the subclass, even though it appears in the inheritance tree more than once. This must be taken into consideration when designing class hierarchies, particularly if base class constructor parameters for that class are explicitly provided in a different way by the inheriting classes.

Note
Class members only exist once for each object; therefore if classes in an inheritance tree have different uses for members with the same name, then a class hierarchy built of such classes will probably not function properly.

Subclasses can give explicit arguments to their base class constructors using a special syntax (only available to subclass constructors) similar to the C++ syntax for the same purpose as follows:
    [[private[:internal|hierarchy]|public] [deprecated] constructor([[param_type] $param_name [= default_initialization_expression], ...]) [: parent_class_name(args...), ...] {
    }]

Here is a concrete example of giving arguments to an inherited base class:

class XmlRpcClient inherits Qore::HTTPClient {
# calls the base class HTTPClient constructor, overrides the "protocols" key to "xmlrpc"
constructor(hash opts = hash()) : Qore::HTTPClient(opts + ( "protocols" : "xmlrpc" )) {
}
}
Qore namespace.
Definition: QC_AbstractSmartLock.dox.h:2

Because base class constructors are executed before subclass constructors, the only local variables in the constructor that can be referenced are those declared in the subclass constructor declaration (if any). What this means is that if you declare local variables in the expressions giving base class arguments, these local variables are not accessible from the constructor body.

Note
Base classes that give explicit arguments to their base class constructors can be overridden by subclasses by simply listing the base class in the base class constructor list and providing new arguments.

Private Inheritance

Classes inherited using the private access modifier encapsulate the functionality (members, constants, methods, etc) of the parent class privately in the class hierarchy; that is, any access to the privately-inherited parent class's functionality from outside the class hierarchy will result in either a parse-time or runtime exception, depending on when the error is caught. When inheritance is declared private, the inherted class is accessible in all child classes in the hierarchy.

Classes inherited using the private:internal access modifier limit access to the inherited class to the child class; the inherited class in this case is not available in the rest of the class hierarchy but is limited to the directly inheriting child class.

Public Inheritance

Classes are inherited publically by default; public inheritance means that the parent class's functionality (members, constants, methods, etc) have the same visibility in the child class as they do in the parent class. For example, a public method in the parent class will also be public in the child class if the parent class is inherited publically. Respectively, private methods of publically-inherited parent classes will still be private in child classes.