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 ? 7 2507
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 ?
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
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.
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
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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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() ; ++) { /* ... */ }
|
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)
|
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 ;
|
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>
| |
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...
|
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;
}
|
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
|
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.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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,...
| |
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...
|
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...
|
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,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |