native vs value struct

  • Thread starter dragonslayer008
  • Start date
D

dragonslayer008

1. Can a value struct inherit from a native struct?

2. Suppose I have a native structure:

struct Vector3
{
float x, y, z;
};

and you make a corresponding managed version

value struct VectorEx3
{
float x, y, z;
};

If you have a function that takes a pointer to a Vector3 as a
parameter, is it okay to cast a VectorEx3 object to it:

VectorEx3 v;
foo( (Vector3*)&v );

I tried it and it worked, but I want to make sure it is defined
behavior.
 
B

Ben Voigt [C++ MVP]

1. Can a value struct inherit from a native struct?

No, a .NET value struct can never have a base class, only pure interfaces.
2. Suppose I have a native structure:

struct Vector3
{
float x, y, z;
};

and you make a corresponding managed version

value struct VectorEx3
{
float x, y, z;
};

If you have a function that takes a pointer to a Vector3 as a
parameter, is it okay to cast a VectorEx3 object to it:

VectorEx3 v;
foo( (Vector3*)&v );

I tried it and it worked, but I want to make sure it is defined
behavior.

It isn't guaranteed to work, and even adding the SequentialLayout attribute
only guarantees compatibility when passed through p/invoke. That said, as
long as you stick to the built-in numeric types, and structures thereof, I
don't think you'll run into any trouble.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top