Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Classes and Structs (Managed) Page 1 of 2

Collapse All Code: All


Visual C++ Language Reference
Classes and Structs (Managed)
Example See Also Send Feedback

Declares a managed class or struct.

class_access ref class name modifier : inherit_access base_type { };


class_access ref struct name modifier : inherit_access base_type {};
class_access value class name modifier : inherit_access base_type {};
class_access value struct name modifier : inherit_access base_type {};

Parameters

base_type(optional)
A base type. A ref class or ref struct can inherit from zero or more managed interfaces and zero or
one ref types. A value class or value struct can only inherit from zero or more managed interfaces.
class_access (optional)
The accessibility of the class or struct outside the assembly. Possible values are public and private.
private is the default. Nested classes or structs cannot have a class_access specifier.
inherit_access (optional)
The accessibility of base_type. The only permitted accessibility for a base type is public (public is
the default).
modifier (optional)
abstract and sealed are valid modifiers.
name
The name of the class or struct.

Remarks

Like their native equivalents, default member accessibility of a ref class or value class is private and
default member accessibility of a ref struct or value struct is public.

A value type cannot act as a base type.

When a reference type inherits from another reference type, virtual functions in the base class must
explicitly be overridden (with override) or hidden (with new (new slot in vtable)). The derived class
functions must also be explicitly marked as virtual.

For more information on classes and structs, see


z Instantiating Classes and Structs

z Semantics of the this Pointer in Value and Reference Types


z C++ Stack Semantics for Reference Types

z Classes, Structures, and Unions

z public and private on Native Classes

z Implicitly Abstract Classes

z Define Static Constructors in a Class or Struct

z Copy Constructor May Not Be Generated

z Hide-by-Signature Functions in Reference Types

z Destructors and Finalizers in Visual C++

z Type and Member Visibility

z User-Defined Operators

z User-Defined Conversions
z How to: Wrap Native Class for Use by C#

z Generic Classes (Visual C++)

ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/dv_vclang/html/5c360764-b229-49c6-93... 5/5/2010
Classes and Structs (Managed) Page 2 of 2

For information on interfaces, see interface class.

You can detect at compile time if a type is a CLR type with __is_ref_class (type),
__is_value_class (type), or __is_simple_value_class (type). For more information, see Compiler
Support for Type Traits.

In the development environment, you can get F1 help on these keywords by highlighting the keyword,
(ref class, for example) and pressing F1.

Example
Copy Code

// mcppv2_ref_class.cpp
// compile with: /clr /LD
ref class MyClass {
public:
int i;

ref class MyClass2 {


public:
int i;
};

interface struct MyInterface {


void f();
};
};

ref class MyClass2 : MyClass::MyInterface {


public:
virtual void f() {
System::Console::WriteLine("test");
}
};

value struct MyStruct {


void f() {
System::Console::WriteLine("test");
}
};

Requirements

Compiler option: /clr

See Also
Concepts
Language Features for Targeting the CLR

Send feedback on this topic to Microsoft.

ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/dv_vclang/html/5c360764-b229-49c6-93... 5/5/2010

You might also like