![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Since implement the assign operator for reference types eliminates the ability to assign a reference object to a reference variable of the same type or base class of that type, I assume that implementing the assign operator ( = ), to assign the value of a type from one object to another rather than the reference, should only be done for value types. Is there any other reason for implementing the assign operator for a type ? |
#3
| |||
| |||
|
|
Since implement the assign operator for reference types eliminates the ability to assign a reference object to a reference variable |
|
to assign the value of a type from one object to another rather than the reference, should only be done for value types. |
#4
| |||
| |||
|
|
Edward Diener wrote: Since implement the assign operator for reference types eliminates the ability to assign a reference object to a reference variable I don't think so. Assigning handles is like assigning pointers in unmanaged code. operator= should be implemented to work on references. MyRefClass^ object1; MyRefClass^ object2; object1 = object2; // assigns handles, does not copy the object *object1 = *object2; // this calls operator= |
|
With the stack syntax, you can assign objects in a more comfortable manner: MyRefClass object1, object2; object1 = object2; // this calls operator= |
|
Either way, it only works in languages that support operator overloading (such as C++/CLI). to assign the value of a type from one object to another rather than the reference, should only be done for value types. No, the opposite. It can only be done to ref classes. You can't have an assignment operator for a value type, just like you can't have a copy constructor either. Value types in .NET are like PODs in C++. When you assign a value type, the framework does a raw memory memcpy. You can't have custom assignment behavior there. |
#5
| |||
| |||
|
|
MyRefClass^ object1; MyRefClass^ object2; object1 = object2; // assigns handles, does not copy the object *object1 = *object2; // this calls operator= This is different from all other operators. Are you sure this is how the assignment operator works ? |
|
OK, I assume then I will get a compiler error if I try to implement the assignment error for a value type. |
#6
| |||
| |||
|
|
This is different from all other operators. Are you sure this is how the assignment operator works ? |
perator=");
perator=");
#7
| |||
| |||
|
|
Edward Diener wrote: MyRefClass^ object1; MyRefClass^ object2; object1 = object2; // assigns handles, does not copy the object *object1 = *object2; // this calls operator= This is different from all other operators. Are you sure this is how the assignment operator works ? Yes, if it's implemented correctly. In ISO C++ it should be T& operator=(const T&); In C++/CLI it should be T% operator=(T%); or T% operator=(const T%); |
#8
| |||
| |||
|
|
By analogy are you also saying that other operators should act the same ? So that for a + operator for a ref class I should have: ref class X { public: static X% operator + ( X% first, X% second ) { // some code returning a tracking reference to a new X } // rather than public: static X^ operator + ( X^ first, X^ second ) { // some code returning a handle to a new X } }; |
|
This is what I meant by the operator = being different from other C++/CLI operators. |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |