473,386 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 2496
It can be useful for plumbing types. See msclr/auto_handle.h for an example.

Marcus

"Edward Diener" <ed*******************@tropicsoft.com> wrote in message
news:en**************@TK2MSFTNGP04.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=(MyRefClass% source)
{
Console::WriteLine("MyRefClass::operator=");
return *this;
}
};

int main(array<System::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=(MyRefClass^ source)
// malformed operator=
{
Console::WriteLine("BAD MyRefClass::operator=");
return *this;
}
};

int main(array<System::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
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...
4
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...
3
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 ...
18
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
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...
13
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...
2
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
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...
8
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.