Skip to content

Latest commit

 

History

History
97 lines (66 loc) · 3.38 KB

abstract-cpp-component-extensions.md

File metadata and controls

97 lines (66 loc) · 3.38 KB
descriptiontitlems.datems.topicf1_keywordshelpviewer_keywordsms.assetid
Learn more about: abstract (C++/CLI and C++/CX)
abstract (C++/CLI and C++/CX)
10/12/2018
reference
abstract
abstract_cpp
abstract keyword [C++]
cbae3408-0378-4ac8-b70d-c016b381a6d5

abstract (C++/CLI and C++/CX)

The abstract keyword declares either:

  • A type can be used as a base type, but the type itself cannot be instantiated.

  • A type member function can be defined only in a derived type.

All Platforms

Syntax

class-declarationclass-identifierabstract {}

virtualreturn-typemember-function-identifier() abstract ;

Remarks

The first example syntax declares a class to be abstract. The class-declaration component can be either a native C++ declaration (class or struct), or a C++ extension declaration (ref class or ref struct) if the /ZW or /clr compiler option is specified.

The second example syntax declares a virtual member function to be abstract. Declaring a function abstract is the same as declaring it a pure virtual function. Declaring a member function abstract also causes the enclosing class to be declared abstract.

The abstract keyword is supported in native and platform-specific code; that is, it can be compiled with or without the /ZW or /clr compiler option.

You can detect at compile time if a type is abstract with the __is_abstract(type) type trait. For more information, see Compiler Support for Type Traits.

The abstract keyword is a context-sensitive override specifier. For more information about context-sensitive keywords, see Context-Sensitive Keywords. For more information about override specifiers, see How to: Declare Override Specifiers in Native Compilations.

Windows Runtime

For more information, see Ref classes and structs.

Requirements

Compiler option: /ZW

Common Language Runtime

Requirements

Compiler option: /clr

Examples

The following code example generates an error because class X is marked abstract.

// abstract_keyword.cpp// compile with: /clr ref classX abstract { public:virtualvoidf() {} }; intmain() { X ^ MyX = gcnew X; // C3622 }

The following code example generates an error because it instantiates a native class that is marked abstract. This error will occur with or without the /clr compiler option.

// abstract_keyword_2.cppclassX abstract { public:virtualvoidf() {} }; intmain() { X * MyX = new X; // C3622: 'X': a class declared as 'abstract'// cannot be instantiated. See declaration of 'X'}

The following code example generates an error because function f includes a definition but is marked abstract. The final statement in the example shows that declaring an abstract virtual function is equivalent to declaring a pure virtual function.

// abstract_keyword_3.cpp// compile with: /clr ref classX { public:virtualvoidf() abstract {} // C3634virtualvoidg() = 0 {} // C3634 };

See also

Component Extensions for .NET and UWP

close