Classes and Structs (Managed)
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 {};
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
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.
// 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");
}
};