__declspec(delphiclass)

From RAD Studio
Jump to: navigation, search

Go Up to Keywords, Alphabetical Listing Index


Category

Modifiers (C++), Keyword Extensions, Storage Class Specifiers (C++)

Syntax

__declspec( delphiclass ) declarator

Description

The delphiclass argument is used to declare Delphi-style classes. Such classes are created with the following compatibility:

  • Delphi-compatible RTTI
  • Delphi-compatible exception handling
  • Delphi-compatible constructor/destructor behavior
  • AfterConstruction and BeforeDestruction procedures (code that in regular C++ classes would be placed in constructors and destructors, respectively)

There are the following restrictions for Delphi-style classes:

  • No virtual base classes are allowed.
  • No multiple inheritance is allowed except for the case described in Inheritance and Interfaces.
  • It must be dynamically allocated by using the global new operator.
  • Delphi-style instances are zero-initialized, so that their state is defined before executing any constructor or method. Without this feature, invoking methods before constructors can yield unpredictable results.
  • Copy constructors and assignment operators are not compiler-generated for Delphi-style derived classes.
  • A forward class declaration that is translated from Delphi requires the delphiclass modifier if the compiler needs to know that the class is Delphi-style. (For example, if the class ID or other RTTI information is necessary before the class definition.)

Other remarks:

  • Unlike regular C++ classes, the constructors and destructors of Delphi-style base classes can invoke virtual methods and run the "most derived" implementations. Thus, it is possible that a method of a class is executed before any constructor.

Example

The following example declares a Delphi-style class with a class method that returns the class name.

#include <System.hpp>
#include <stdio.h>
#include <tchar.h>

class __declspec(delphiclass) TLock : public TObject {
public:
	virtual __classmethod UnicodeString GetClsName() {
		return this->ClassName();
	}
};

int _tmain(int argc, _TCHAR* argv[]) {
	printf("%ls", TLock::GetClsName().c_str()); // displays TLock
	return 0;
}

See Also