Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / December 2006

Tip: Looking for answers? Try searching our database.

Managed Overloaded Assignment Operator needed

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
raylopez99 - 30 Dec 2006 00:02 GMT
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);
raylopez99 - 30 Dec 2006 09:41 GMT
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

> 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;.
raylopez99 - 30 Dec 2006 14:32 GMT
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 "="

*/

> 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
[quoted text clipped - 43 lines]
>     //void operator =(const ARefClass ^rhs){}; //compiles
>     ARefClass^ ARefClass::operator =(const ARefClass ^rhs);
raylopez99 - 30 Dec 2006 21:01 GMT
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

> 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
[quoted text clipped - 77 lines]
> >     //void operator =(const ARefClass ^rhs){}; //compiles
> >     ARefClass^ ARefClass::operator =(const ARefClass ^rhs);
Marcus Heege - 31 Dec 2006 07:35 GMT
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

> Just to complete this thread, I found you can overload the "+" operator
> as well, for non-pointer (tracking pointer) instantiated reference
[quoted text clipped - 119 lines]
>> > //void operator =(const ARefClass ^rhs){}; //compiles
>> > ARefClass^ ARefClass::operator =(const ARefClass ^rhs);
raylopez99 - 31 Dec 2006 14:31 GMT
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

> Hi raylopez99,
>
[quoted text clipped - 130 lines]
> >> > //void operator =(const ARefClass ^rhs){}; //compiles
> >> > ARefClass^ ARefClass::operator =(const ARefClass ^rhs);

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.