472,958 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Managed Overloaded Assignment Operator needed

I need an example of a managed overloaded assignment operator for a
reference class, so I can equate two classes A1 and A2, say called
ARefClass, in this manner: A1=A2;. For some strange reason my C++.NET
2.0 textbook does not have one. I tried to build one using the format
as taught in my regular C++ book, but I keep getting compiler errors.

Some errors claim (contrary to my book) that you cannot use a static
function, that you must use a unitary operator for an assignment, etc.

for example, in traditional C++, this should work:

ARefClass ARefClass::operator=(ARefClass AAA) //AAA has a public
variable X
{
X = AAA.X;
return *this;
}

For managed C++, I would imagine this might work:

ARefClass ARefClass::operator=(ARefClass AAA) //AAA has a public
variable X

ARefClass^ ARefClass::operator =(const ARefClass ^rhs)
{
ARefClass ^ret = gcnew ARefClass();
ret->X = rhs->X;
return ret;
}

But it fails to compile, C2582: An attempt was made to assign to an
object that does not have an assignment operator; and error C2679:
binary '=' : no operator found which takes a right-hand operand of type
'AClass' (or there is no acceptable conversion)

Any ideas? I've also tried other permutations of the above with no
luck (below are some examples).

Thanks!

RL
//ARefClass^ operator =(const ARefClass ^lhs, const ARefClass ^rhs);
// assignment operator (binary operator?) only non-static allowed
// void operator =(const ARefClass ^lhs, const ARefClass ^rhs){};
//void operator =(const ARefClass ^rhs){}; //compiles
ARefClass^ ARefClass::operator =(const ARefClass ^rhs);

Dec 30 '06 #1
5 2220
It's interesting that I do have a copy constructor that gets involked
when doing things like ARefClass A2 = A1; or ARefClass A2(A1);

Thus using this copy constructor I could effectively do the same thing
as an assignment operator, but I would like to see if I can do things
like: A2 = A1 + A0; (just thought of this: If I overload the "+" do I
need to overload the "=" in a reference class? I'll have to research
this later today).

Ironically the earlier version of C++.NET used a more bizarre keyword
"op_Assign", which in some ways is a step forward (reminds the
programmer that the overloaded assignment operator is being used).

RL

raylopez99 wrote:
I need an example of a managed overloaded assignment operator for a
reference class, so I can equate two classes A1 and A2, say called
ARefClass, in this manner: A1=A2;.
Dec 30 '06 #2
I think I found the answer: there is no overloaded assignment operator
for reference classes in managed C++.NET 2.0 (for most ordinary uses at
least).

This works:

AClass^ AClass::operator+ (AClass^ prhs); //definition in header file

// declaration in .cpp file
AClass^ AClass::operator+ (AClass^ prhs) //Note: in managed C++.NET
2.0, overloaded "+" is a unitary operator, not a binary operator!
{
AClass^ result = gcnew AClass(this->pubint + prhs->pubint); //using
parametrized normal constructor (not shown here)
// can do other things to result here regarding any embedded
composition classes within AClass result here
return result;
}

// Note: overloaded "=" not necessary in managed reference classes
(for most normal purposes).

/*

Hence, using the above, we can do the following without a overloaded
"="

AClass ^x1 = gcnew AClass(22);
AClass ^x2 = gcnew AClass(33);
AClass ^x3;
x3 = x1 + x2; //works! No need to overload "="

*/
raylopez99 wrote:
I need an example of a managed overloaded assignment operator for a
reference class, so I can equate two classes A1 and A2, say called
ARefClass, in this manner: A1=A2;. For some strange reason my C++.NET
2.0 textbook does not have one. I tried to build one using the format
as taught in my regular C++ book, but I keep getting compiler errors.

Some errors claim (contrary to my book) that you cannot use a static
function, that you must use a unitary operator for an assignment, etc.

for example, in traditional C++, this should work:

ARefClass ARefClass::operator=(ARefClass AAA) //AAA has a public
variable X
{
X = AAA.X;
return *this;
}

For managed C++, I would imagine this might work:

ARefClass ARefClass::operator=(ARefClass AAA) //AAA has a public
variable X

ARefClass^ ARefClass::operator =(const ARefClass ^rhs)
{
ARefClass ^ret = gcnew ARefClass();
ret->X = rhs->X;
return ret;
}

But it fails to compile, C2582: An attempt was made to assign to an
object that does not have an assignment operator; and error C2679:
binary '=' : no operator found which takes a right-hand operand of type
'AClass' (or there is no acceptable conversion)

Any ideas? I've also tried other permutations of the above with no
luck (below are some examples).

Thanks!

RL
//ARefClass^ operator =(const ARefClass ^lhs, const ARefClass ^rhs);
// assignment operator (binary operator?) only non-static allowed
// void operator =(const ARefClass ^lhs, const ARefClass ^rhs){};
//void operator =(const ARefClass ^rhs){}; //compiles
ARefClass^ ARefClass::operator =(const ARefClass ^rhs);
Dec 30 '06 #3
Just to complete this thread, I found you can overload the "+" operator
as well, for non-pointer (tracking pointer) instantiated reference
class objects--but, if you do that, you must also overload the
assignment operator (unlike the case for reference classes that are
instantiated with pointers).

So here is the code for such reference classes

// in header
AClass AClass::operator+ (AClass rhs); //overloaded operator for ref
class not a tracking ptr
AClass AClass::operator= (AClass rhs); //for above line only
(non-tracking ptr)

// in .CPP

AClass AClass::operator+ (AClass rhs)
{
AClass result(this->pubint + rhs.pubint); //using a parametrized
normal construtor (not shown) to instantiate the return value 'AClass
result'
//other members here that need to be added
return result;
}

AClass AClass::operator =(AClass rhs)
{
pubint = rhs.pubint; //equating a member variable
privint = rhs.privint; // " "
privs1 = rhs.privs1; // " "
//other members equalized here, as need be, to equalize the classes,
etc
return *this;
}

// in main.cpp

AClass A, B, C; // reference classes instantiated without tracking
pointers--compare with before in this thread

A = B + C; //now works fine

raylopez99 wrote:
I think I found the answer: there is no overloaded assignment operator
for reference classes in managed C++.NET 2.0 (for most ordinary uses at
least).

This works:

AClass^ AClass::operator+ (AClass^ prhs); //definition in header file

// declaration in .cpp file
AClass^ AClass::operator+ (AClass^ prhs) //Note: in managed C++.NET
2.0, overloaded "+" is a unitary operator, not a binary operator!
{
AClass^ result = gcnew AClass(this->pubint + prhs->pubint); //using
parametrized normal constructor (not shown here)
// can do other things to result here regarding any embedded
composition classes within AClass result here
return result;
}

// Note: overloaded "=" not necessary in managed reference classes
(for most normal purposes).

/*

Hence, using the above, we can do the following without a overloaded
"="

AClass ^x1 = gcnew AClass(22);
AClass ^x2 = gcnew AClass(33);
AClass ^x3;
x3 = x1 + x2; //works! No need to overload "="

*/
raylopez99 wrote:
I need an example of a managed overloaded assignment operator for a
reference class, so I can equate two classes A1 and A2, say called
ARefClass, in this manner: A1=A2;. For some strange reason my C++.NET
2.0 textbook does not have one. I tried to build one using the format
as taught in my regular C++ book, but I keep getting compiler errors.

Some errors claim (contrary to my book) that you cannot use a static
function, that you must use a unitary operator for an assignment, etc.

for example, in traditional C++, this should work:

ARefClass ARefClass::operator=(ARefClass AAA) //AAA has a public
variable X
{
X = AAA.X;
return *this;
}

For managed C++, I would imagine this might work:

ARefClass ARefClass::operator=(ARefClass AAA) //AAA has a public
variable X

ARefClass^ ARefClass::operator =(const ARefClass ^rhs)
{
ARefClass ^ret = gcnew ARefClass();
ret->X = rhs->X;
return ret;
}

But it fails to compile, C2582: An attempt was made to assign to an
object that does not have an assignment operator; and error C2679:
binary '=' : no operator found which takes a right-hand operand of type
'AClass' (or there is no acceptable conversion)

Any ideas? I've also tried other permutations of the above with no
luck (below are some examples).

Thanks!

RL
//ARefClass^ operator =(const ARefClass ^lhs, const ARefClass ^rhs);
// assignment operator (binary operator?) only non-static allowed
// void operator =(const ARefClass ^lhs, const ARefClass ^rhs){};
//void operator =(const ARefClass ^rhs){}; //compiles
ARefClass^ ARefClass::operator =(const ARefClass ^rhs);
Dec 30 '06 #4
Hi raylopez99,

to overload an assignment operator look at the definition of
msclr::auto_handle in msclr/auto_handle.h.

Howver, you should be aware that this only works in the context of
implicitly dereferenced variables (e.g. Stack-like) variables. Therefore, it
is likely not what you want.

Marcus

"raylopez99" <ra********@yahoo.comwrote in message
news:11*********************@a3g2000cwd.googlegrou ps.com...
Just to complete this thread, I found you can overload the "+" operator
as well, for non-pointer (tracking pointer) instantiated reference
class objects--but, if you do that, you must also overload the
assignment operator (unlike the case for reference classes that are
instantiated with pointers).

So here is the code for such reference classes

// in header
AClass AClass::operator+ (AClass rhs); //overloaded operator for ref
class not a tracking ptr
AClass AClass::operator= (AClass rhs); //for above line only
(non-tracking ptr)

// in .CPP

AClass AClass::operator+ (AClass rhs)
{
AClass result(this->pubint + rhs.pubint); //using a parametrized
normal construtor (not shown) to instantiate the return value 'AClass
result'
//other members here that need to be added
return result;
}

AClass AClass::operator =(AClass rhs)
{
pubint = rhs.pubint; //equating a member variable
privint = rhs.privint; // " "
privs1 = rhs.privs1; // " "
//other members equalized here, as need be, to equalize the classes,
etc
return *this;
}

// in main.cpp

AClass A, B, C; // reference classes instantiated without tracking
pointers--compare with before in this thread

A = B + C; //now works fine

raylopez99 wrote:
>I think I found the answer: there is no overloaded assignment operator
for reference classes in managed C++.NET 2.0 (for most ordinary uses at
least).

This works:

AClass^ AClass::operator+ (AClass^ prhs); //definition in header file

// declaration in .cpp file
AClass^ AClass::operator+ (AClass^ prhs) //Note: in managed C++.NET
2.0, overloaded "+" is a unitary operator, not a binary operator!
{
AClass^ result = gcnew AClass(this->pubint + prhs->pubint); //using
parametrized normal constructor (not shown here)
// can do other things to result here regarding any embedded
composition classes within AClass result here
return result;
}

// Note: overloaded "=" not necessary in managed reference classes
(for most normal purposes).

/*

Hence, using the above, we can do the following without a overloaded
"="

AClass ^x1 = gcnew AClass(22);
AClass ^x2 = gcnew AClass(33);
AClass ^x3;
x3 = x1 + x2; //works! No need to overload "="

*/
raylopez99 wrote:
I need an example of a managed overloaded assignment operator for a
reference class, so I can equate two classes A1 and A2, say called
ARefClass, in this manner: A1=A2;. For some strange reason my C++.NET
2.0 textbook does not have one. I tried to build one using the format
as taught in my regular C++ book, but I keep getting compiler errors.

Some errors claim (contrary to my book) that you cannot use a static
function, that you must use a unitary operator for an assignment, etc.

for example, in traditional C++, this should work:

ARefClass ARefClass::operator=(ARefClass AAA) //AAA has a public
variable X
{
X = AAA.X;
return *this;
}

For managed C++, I would imagine this might work:

ARefClass ARefClass::operator=(ARefClass AAA) //AAA has a public
variable X

ARefClass^ ARefClass::operator =(const ARefClass ^rhs)
{
ARefClass ^ret = gcnew ARefClass();
ret->X = rhs->X;
return ret;
}

But it fails to compile, C2582: An attempt was made to assign to an
object that does not have an assignment operator; and error C2679:
binary '=' : no operator found which takes a right-hand operand of type
'AClass' (or there is no acceptable conversion)

Any ideas? I've also tried other permutations of the above with no
luck (below are some examples).

Thanks!

RL
//ARefClass^ operator =(const ARefClass ^lhs, const ARefClass ^rhs);
// assignment operator (binary operator?) only non-static allowed
// void operator =(const ARefClass ^lhs, const ARefClass ^rhs){};
//void operator =(const ARefClass ^rhs){}; //compiles
ARefClass^ ARefClass::operator =(const ARefClass ^rhs);

Dec 31 '06 #5
Hi Marcus,

I took a quick look at the auto_handle.h header definition; thanks,
>From the example in this thread it seems the overloaded assignment
operator is working for my purposes, so I don't quite understand your
statement "Therefore, it is likely not what you want."

Can you give an example of non-stack-like (heap) variables?

Below are some examples of both stack-like and heap-like (tracking
pointer/handles used) reference class examples where the "+" and "="
are overloaded, as per this thread.

//////////////////////////
/*
AClass is a reference class having a public member variable 'pubint'.
It also has a copy constructor, and two overloaded operators for "+",
but only one overload operator for "=" (not needed for
tracking-pointer/handle version)

Refer to this thread for definitions and declarations

*/

// first example, using tracking-pointer/handles to instantiate AClass,
heap-like?

AClass ^x4A = gcnew AClass;
//or AClass ^x4A; gives same thing (no need to initialize)
AClass ^x5A = gcnew AClass(40);
AClass ^x6A = gcnew AClass(60);
x4A = x5A->operator+(x6A); //equivalent to x4A = x5A + x6A; //also
works
x4A->pubint == 100; //gives output "100" for pubint, as
expected
/* in this example, no copy constructor called and ONLY the
"+" overloaded operator called; the overloaded assignment operator "="
is not called */

// second example, using normal constructor (no
tracking-pointer/handles)-stack-like?
AClass AA(999);
AClass BB(1000);
AClass CC(1001);
CC = AA + BB; //gives output, for 'pubint', of 1999 (not 1001), as
expected
//copy constructor called and overloaded operators "+" and
"=" both called.

// third example, using "%" indirection operator and tracking pointer
-stack-like?

AClass DD(12), DD2(133), DD3;
AClass ^pDD, ^pDD2;

pDD=%DD;
pDD2=%DD2;
DD3 = *pDD + *pDD2;
//DD3.pubint == 145 //(12 + 133). //note dot operator not
'->' used to access pubint
//copy constructor called and overloaded operators "+" and
"=" both called, like ex. 2

//////////////////////////
The only downside to all of this is that lots of coding has to be done
(to deep copy over each member of a class comprising a composition,
that can include other classes). Lots of times you can let the normal
constructor do the copying, when inside the overloaded operators (see
this thread, note at the normal constructor or at gcnew, input the
parameters you want copied), but for arrays that are composite members,
you have to manually copy each element of the array (seems to me).
Lots of work therefore. Also for the non-tracking pointer/handle
version (second and third example above) the copy constructor gets
called a lot. The copy constructor also of course has to do a deep
copy of every member in the class AClass, which is tedious to code.
Also, you can use 'const' in the definition/declaration of each
overloaded operator, i.e.:

AClass^ AClass::operator+ (const AClass^ prhs); //compiles and works
fine.
AClass AClass::operator+ (const AClass rhs); //compiles and works fine

AClass AClass::operator= (const AClass rhs); //" ", used with above
line only, not with AClass^

Am I missing some hidden potential bug in all of this?

Please let me know.

RL

Marcus Heege wrote:
Hi raylopez99,

to overload an assignment operator look at the definition of
msclr::auto_handle in msclr/auto_handle.h.

Howver, you should be aware that this only works in the context of
implicitly dereferenced variables (e.g. Stack-like) variables. Therefore, it
is likely not what you want.

Marcus

"raylopez99" <ra********@yahoo.comwrote in message
news:11*********************@a3g2000cwd.googlegrou ps.com...
Just to complete this thread, I found you can overload the "+" operator
as well, for non-pointer (tracking pointer) instantiated reference
class objects--but, if you do that, you must also overload the
assignment operator (unlike the case for reference classes that are
instantiated with pointers).

So here is the code for such reference classes

// in header
AClass AClass::operator+ (AClass rhs); //overloaded operator for ref
class not a tracking ptr
AClass AClass::operator= (AClass rhs); //for above line only
(non-tracking ptr)

// in .CPP

AClass AClass::operator+ (AClass rhs)
{
AClass result(this->pubint + rhs.pubint); //using a parametrized
normal construtor (not shown) to instantiate the return value 'AClass
result'
//other members here that need to be added
return result;
}

AClass AClass::operator =(AClass rhs)
{
pubint = rhs.pubint; //equating a member variable
privint = rhs.privint; // " "
privs1 = rhs.privs1; // " "
//other members equalized here, as need be, to equalize the classes,
etc
return *this;
}

// in main.cpp

AClass A, B, C; // reference classes instantiated without tracking
pointers--compare with before in this thread

A = B + C; //now works fine

raylopez99 wrote:
I think I found the answer: there is no overloaded assignment operator
for reference classes in managed C++.NET 2.0 (for most ordinary uses at
least).

This works:

AClass^ AClass::operator+ (AClass^ prhs); //definition in header file

// declaration in .cpp file
AClass^ AClass::operator+ (AClass^ prhs) //Note: in managed C++.NET
2.0, overloaded "+" is a unitary operator, not a binary operator!
{
AClass^ result = gcnew AClass(this->pubint + prhs->pubint); //using
parametrized normal constructor (not shown here)
// can do other things to result here regarding any embedded
composition classes within AClass result here
return result;
}

// Note: overloaded "=" not necessary in managed reference classes
(for most normal purposes).

/*

Hence, using the above, we can do the following without a overloaded
"="

AClass ^x1 = gcnew AClass(22);
AClass ^x2 = gcnew AClass(33);
AClass ^x3;
x3 = x1 + x2; //works! No need to overload "="

*/
raylopez99 wrote:
I need an example of a managed overloaded assignment operator for a
reference class, so I can equate two classes A1 and A2, say called
ARefClass, in this manner: A1=A2;. For some strange reason my C++.NET
2.0 textbook does not have one. I tried to build one using the format
as taught in my regular C++ book, but I keep getting compiler errors.

Some errors claim (contrary to my book) that you cannot use a static
function, that you must use a unitary operator for an assignment, etc.

for example, in traditional C++, this should work:

ARefClass ARefClass::operator=(ARefClass AAA) //AAA has a public
variable X
{
X = AAA.X;
return *this;
}

For managed C++, I would imagine this might work:

ARefClass ARefClass::operator=(ARefClass AAA) //AAA has a public
variable X

ARefClass^ ARefClass::operator =(const ARefClass ^rhs)
{
ARefClass ^ret = gcnew ARefClass();
ret->X = rhs->X;
return ret;
}

But it fails to compile, C2582: An attempt was made to assign to an
object that does not have an assignment operator; and error C2679:
binary '=' : no operator found which takes a right-hand operand of type
'AClass' (or there is no acceptable conversion)

Any ideas? I've also tried other permutations of the above with no
luck (below are some examples).

Thanks!

RL
//ARefClass^ operator =(const ARefClass ^lhs, const ARefClass ^rhs);
// assignment operator (binary operator?) only non-static allowed
// void operator =(const ARefClass ^lhs, const ARefClass ^rhs){};
//void operator =(const ARefClass ^rhs){}; //compiles
ARefClass^ ARefClass::operator =(const ARefClass ^rhs);
Dec 31 '06 #6

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

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
5
by: Andy Jarrell | last post by:
I'm trying to inherit from a specific class that has an overloaded operator. The problem I'm getting is that certain overloaded operators don't seem to come with the inheritance. For example: ...
4
by: August1 | last post by:
I've written an interface and implementation file along with a client source file that allows the use of an overloaded subtraction operator. However, when using the program, I'm running into a...
2
by: Tony Johansson | last post by:
Hello Experts!! I have two small classes called Intvektor and Matris shown at the bottom and a main. Class Intvektor will create a one dimension array of integer by allocate memory dynamically...
12
by: Jack Daly | last post by:
I've inherited some code which uses an undocumented feature of a third-party vendor's library. Essentially, this vendor has kept the details of an interface struct secret, but we can pass a pointer...
2
by: B. Williams | last post by:
I have an assignment for school to Overload the operators << and >and I have written the code, but I have a problem with the insertion string function. I can't get it to recognize the second of...
5
by: sam_cit | last post by:
Hi Everyone, I was just wondering, about the overloaded assignment operator for user defined objects. It is used to make sure that the following works properly, obj1 = obj; so the...
2
by: subramanian100in | last post by:
overloaded operator=() -------------------------------- overloaded assignment operator should be a non-static MEMBER function of a class. This ensures that the first operand is an lvalue. If...
3
by: jr.freester | last post by:
I have created to classes Matrix and System. System is made up of type matrix. ---------------------------------------------------------------------------------- class Matrix { private: int...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.