R
raylopez99
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:perator=(ARefClass AAA) //AAA has a public
variable X
{
X = AAA.X;
return *this;
}
For managed C++, I would imagine this might work:
ARefClass ARefClass:perator=(ARefClass AAA) //AAA has a public
variable X
ARefClass^ ARefClass:perator =(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:perator =(const ARefClass ^rhs);
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:perator=(ARefClass AAA) //AAA has a public
variable X
{
X = AAA.X;
return *this;
}
For managed C++, I would imagine this might work:
ARefClass ARefClass:perator=(ARefClass AAA) //AAA has a public
variable X
ARefClass^ ARefClass:perator =(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:perator =(const ARefClass ^rhs);