Hello !
The C++ Standard specifies overloading the assignment operation as follows:
static MyClass^ operator= (MyClass^ opRef)
The new C++/CLI syntax does not take a stand on the assignment overloads. It
is merely an extension to the C++ language, not an alteration of it.
Otherwise, the assignment operation overload follows the rules of the
standard. Check MSDN (normal C++) for more details.
-Antti Keskinen
> Hi!
>
[quoted text clipped - 14 lines]
> ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
> ----------------------------------------------------------
Ioannis Vranos - 03 Mar 2005 15:09 GMT
> Hello !
>
[quoted text clipped - 6 lines]
> Otherwise, the assignment operation overload follows the rules of the
> standard. Check MSDN (normal C++) for more details.
Actually the static method approach is introduced by C++/CLI for managed
classes. ISO C++ (for unmanaged code) approach is either a non-static
method, or a function taking two arguments.
Hi,
I had a problem concerning how to overload the operator = in Microsoft
Visual C++ 2005 Beta.
I was told to do it this way : static Myclass^ operator=(Myclass^);
However, it doesn't work and here is the error message i get when i
compile:
error C2805 : binary operator= has too few parameters
does anybody know the solution?
Sincerely,
bor_kev
Ioannis Vranos - 04 Mar 2005 18:13 GMT
> Hi,
>
[quoted text clipped - 8 lines]
>
> does anybody know the solution?
At first C++/CLI has not been finished yet, and VC++ is beta, so these
things are expected.
Now what the current C++/CLI draft (1.8) says in "18.7 Static operators"
is that the static operators shall take two arguments, while the
unmanaged ISO C++ operators continue to apply.
As far as I can understand this means:
ref class Myclass
{
public:
static Myclass^ operator=(Myclass ^, Myclass ^) {}
};
int main()
{
Myclass ^p1= gcnew Myclass;
Myclass ^p2= gcnew Myclass;
p1= p2;
}
should compile (but it doesn't).
However you can still use the regular C++ operator definitions (but
these can not be used by other .NET languages):
ref class Myclass
{
public:
Myclass^ operator=(Myclass ^) { return this; }
};
int main()
{
Myclass ^p1= gcnew Myclass;
Myclass ^p2= gcnew Myclass;
p1= p2;
}
The above produces 100% IL code since only managed types are used,
however this operator can not be used by other .NET languages.