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++ / March 2005

Tip: Looking for answers? Try searching our database.

Overload operator=

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
bor_kev - 02 Mar 2005 15:52 GMT
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 Keskinen - 03 Mar 2005 12:59 GMT
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.
bor_kev - 04 Mar 2005 09:49 GMT
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.

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.