473,698 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Implementing assign operator ( = )

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 ?
Apr 25 '06 #1
7 2512
It can be useful for plumbing types. See msclr/auto_handle.h for an example.

Marcus

"Edward Diener" <ed************ *******@tropics oft.com> wrote in message
news:en******** ******@TK2MSFTN GP04.phx.gbl...
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 ?

Apr 25 '06 #2
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.

Tom
Apr 25 '06 #3
Tamas Demjen wrote:
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=


This is different from all other operators. Are you sure this is how the
assignment operator works ?

With the stack syntax, you can assign objects in a more comfortable manner:

MyRefClass object1, object2;
object1 = object2; // this calls operator=
This makes sense.

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.


OK, I assume then I will get a compiler error if I try to implement the
assignment error for a value type.
Apr 25 '06 #4
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%);

const is optional here. Since you can't declare a const member function,
passing arguments by const makes little sense (unfortunately) . You would
be forced to cast the const-ness out quite frequently.

But that's irrelevant. The input argument is a T%, not a T^. Just like
in native C++, you don't write operator=(const T*). You don't want to
override the "copy a pointer" behavior. A pointer (or handle) is always
a value type, and making a copy of that should not involve any side
effects or custom behavior. If you're about to define
T^ operator=(T^);
then your original statement is correct, that would be very confusing,
and should be avoided. You don't want to do that, you don't want to
assign a custom behavior to a simple "pass by pointer" syntax.
OK, I assume then I will get a compiler error if I try to implement the
assignment error for a value type.


Yes.

Tom
Apr 25 '06 #5
Edward Diener wrote:
This is different from all other operators. Are you sure this is how the
assignment operator works ?


I don't see how it's different from ISO C++. I just tried this:

ref class MyRefClass
{
public:
MyRefClass% operator=(MyRef Class% source)
{
Console::WriteL ine("MyRefClass ::operator=");
return *this;
}
};

int main(array<Syst em::String ^> ^args)
{
MyRefClass^ object1 = gcnew MyRefClass;
MyRefClass^ object2 = gcnew MyRefClass;
object1 = object2;
}

There's no operator= called. But if I change the last line to *object1 =
*object2, it gets called as expected. Just like in native C++, except
replace handles with pointers.

I also tried to shoot myself in the foot, but the compiler didn't let me:

ref class MyRefClass
{
public:
MyRefClass^ operator=(MyRef Class^ source)
// malformed operator=
{
Console::WriteL ine("BAD MyRefClass::ope rator=");
return *this;
}
};

int main(array<Syst em::String ^> ^args)
{
MyRefClass^ object1 = gcnew MyRefClass;
MyRefClass^ object2 = gcnew MyRefClass;
object1 = object2;
}

operator= is not called, the compiler doesn't let you overload the
handle assignment operator. A handle is a value type, and its assignment
can't be overloaded. In this case you don't get an error -- the
malformed operator= simply won't be called.

Tom
Apr 25 '06 #6
Tamas Demjen wrote:
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%);


OK, I understand this.

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 }

};

and the user should be doing:

X^ first;
X^ second;
X^ result;

*result = *first + *second;

rather than

result = first + second;

This is what I meant by the operator = being different from other
C++/CLI operators. In your assignment operator with a ref class you are
taking tracking references and returning a tracking reference, whereas
the second set of syntaxes above for my hypothetical operator + in a ref
class takes handles and returns a handle, while the first acts like your
assignment operator.
Apr 25 '06 #7
Edward Diener wrote:
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 }

};
You are right, in this case both operators are valid.
This is what I meant by the operator = being different from other
C++/CLI operators.


Yes, I agree.

Tom
Apr 25 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
4330
by: Rim | last post by:
Hi, I have been thinking about how to overload the assign operation '='. In many cases, I wanted to provide users of my packages a natural interface to the extended built-in types I created for them, but the assign operator is always forcing them to "type cast" or coerce the result when they do a simple assign for the purpose of setting the value of a variable. Borrowing an example from this newgroup, the second assignment below ereases...
4
8702
by: Chris Schadl | last post by:
Hi, I've written a simple sorted linked list class that I'm trying to implement an iterator for. I'm trying to make the interface to the iterator simmilar to the STL's iterator interface, so one could do: SortedList<int> sl; SortedList<int>::iterator i; for (i = sl.begin() ; i != sl.end() ; ++) { /* ... */ }
3
1397
by: Tony Johansson | last post by:
Hello! Assume you have a constructor for class AccountForStudent defined in this way AccountForStudent::AccountForStudent(Student s, double balance) : stud_(s), balance_(balance) {} //Here in stud_(s) above we call the copy constructor We can also initialize in this way AccountForStudent::AccountForStudent(Student s, double balance) : stud_(s)
18
4572
by: ineedyourluvin1 | last post by:
Hi, I would appreciate if someone could tell me what I'm doing wrong ? #include<iostream> using namepace std ; struct person{ char *firstname ; int age ;
3
1769
by: Stephen Torri | last post by:
Here is my attempt at implementing a object factory. The purpose of this is to replace a large switch statement in a factory class with the functors. I get an error at line 88, marked, "expected primary-expression before ')' token". I am using Modern C++ Design chapter 8 as a guide. Stephen --------------------- #include <map>
13
5030
by: Tristan Wibberley | last post by:
Hi I've got implementing overloaded operator new and delete pretty much down. Just got to meet the alignment requirements of the class on which the operator is overloaded. But how does one implement operator new/delete I can't see a way to indicate, on delete, how many objects must be destroyed (or how big the space is) - alternatively I can't figure out what are the alignment requirements so that the implementation, after calling my...
2
2835
by: soy.hohe | last post by:
Hi all I have a class StreamLogger which implements operator << in this way: template <class TStreamLogger& operator<<(const T& t) { <print the stuff using fstream, etc.> return *this; }
29
3417
by: stephen b | last post by:
Hi all, personally I'd love to be able to do something like this: vector<intv; v.assign(1, 2, 5, 9, 8, 7) etc without having to manually add elements by doing v = 1, v = 2 .. etc. it would make for much more readable code that is faster to write in some situations. I've not seen this feature documented anywhere
8
2636
by: nickooooola | last post by:
Hello to all I'm about to write a simulator for a microcontroller in python (why python? because I love it!!!) but I have a problem. The registry of this processor are all 8 bit long (and 10 bit for some other strange register) and I need to simulate the fixed point behaviour of the register, and to access the single bit.
0
8612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.