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);