Question

Any good site or explanation on what is a ref class and when to declare a class to be a "ref class"?

The explanation on msdn wasn't enough for me,

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.

ref

The ref keyword tells the compiler that the class or structure will be allocated on the heap and a reference to it will be passed to functions or stored in class members. The value keyword tells the compiler that all of the data in the class or structure is passed to functions or stored in members.

Was it helpful?

Solution

Basically, a ref class is a CLR class. It's the equivalent of class in C#.

This creates a reference type managed by the CLR. If you want to make a class that's usable from C#, you'd normally create a ref class. (ref struct, by the way, does exactly the same thing, but with C++'s standard class vs. struct default accessibility rules.)

Also, just for reference - in order to make a value type (struct in C#), you'd use value class or value struct.

A good explanation of many of these new keywords is Herb Sutter's post on C++/CLI Keywords. This is a useful reference if you're new to C++/CLI, but have a solid C++ background.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top