Sign in to follow this  
Quat

[.net] C++/CLI properties

Recommended Posts

Quat    568
I have my own Vector3 structure, which is a value type. My Camera class has a trivial Vector3 property for its position. This does nothing: mSideCamera->Position.X = 20; mSideCamera->Position.Y = 20; mSideCamera->Position.Z = 20; By nothing, I mean, after these assignments, the Camera's Position property is unchanged. However, this works right: mSideCamera->Position = Vector3(20, 20, 20); Anyone know how to explain this behavior?

Share this post


Link to post
Share on other sites
Mike.Popoloski    3258
Since Vector3 is a value type, the property returns a copy of the information, not a reference. So you are basically modifying the local copy in the first example, which doesn't affect the the copy held within the camera class.

In the second example, you avoid this problem by just assigning the correct value to the position.

I suggest you look up the differences between reference semantics and value semantics. It's quite an important concept for many programming languages.

Share this post


Link to post
Share on other sites
Quat    568
Quote:

Since Vector3 is a value type, the property returns a copy of the information, not a reference. So you are basically modifying the local copy in the first example, which doesn't affect the the copy held within the camera class.

In the second example, you avoid this problem by just assigning the correct value to the position.


I get it. I thought with properties it would automatically implement with references, even for value types (like returning by reference for native types: Vector3&).

Is there a way to have a property return a reference to a value type?

Share this post


Link to post
Share on other sites
Mike.Popoloski    3258
Quote:
Original post by Quat
Quote:

Since Vector3 is a value type, the property returns a copy of the information, not a reference. So you are basically modifying the local copy in the first example, which doesn't affect the the copy held within the camera class.

In the second example, you avoid this problem by just assigning the correct value to the position.


I get it. I thought with properties it would automatically implement with references, even for value types (like returning by reference for native types: Vector3&).

Is there a way to have a property return a reference to a value type?


I don't think so. Is there a reason why you can't just assign all three values at once?

Note that this type of issue crops up in the .NET framework as well. In C#, the compiler will error/warn about it, but in C++/CLI you don't get that luxury. One example of such an issue that immediately comes to mind is the Location property of a Form, which cannot have its X and Y values assigned through the property.

What you could do if you are desperate is to have other properties on your camera class called X, Y, and Z that set the individual elements of the vector internally, but I don't think that's such a good idea in this case.

Share this post


Link to post
Share on other sites
Promit    13246
In general, the .NET way of doing things is to use value types as invariants. That is, it's not considered normal to modify only one piece at a time. It's expected that assignments will be all or nothing, and the properties reflect that idea.

(Language guys: I know I'm playing fast and loose with "invariant". Let it slide, please.)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this