Overload operator=

  • Thread starter Thread starter bor_kev
  • Start date Start date
B

bor_kev

Hi!

What's the syntax to overload the operator= under Microsoft Visual C++
.NET 2005 in a managed class.

I tried : static Myclass^ op_Assign (Myclass^, Myclass^){}
but it doesn't work.

don't know why....


Sincerely

bor_kev
 
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


bor_kev said:
Hi!

What's the syntax to overload the operator= under Microsoft Visual C++
NET 2005 in a managed class.

I tried : static Myclass^ op_Assign (Myclass^, Myclass^){}
but it doesn't work.

don't know why....


Sincerely

bor_kev
 
Antti said:
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.


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
 
bor_kev said:
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?


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.
 
Back
Top