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

Tip: Looking for answers? Try searching our database.

assignment operator syntax

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rob - 24 May 2005 17:22 GMT
To follow up on the "copy constructor clarification thread"...

The assignment operator syntax shown previously:

MyClass% operator=(const MyClass%);

seems to have problems if you have member properties that need to be copied
from the rhs to the lhs. Since the rhs parameter is "const MyClass", you get
an error C2662 "'MyClass::prop::get' : cannot covert 'this' pointer from
'const MyClass' to 'MyClass %'" if you have code inside like

MyClass% MyClass::operator=(const MyClass %m)
{
...
prop = m.type
...
}

I can't make the get on the property return a const int. The compiler
doesn't like that either.

What's the correct syntax for an assignment operator and the copying of a
class's properties? The copy constructor doesn't have this problem since its
parameter isn't const.

-Rob

"Kapil Khosla [MSFT]" wrote:

> "Rob" wrote:
>
> > I'd like to know whether I should add copy and assignment constructors. The
> > standard syntax of
> >     MyClass(const MyClass&);
> >     MyClass& operator=(const MyClass&);
> > doesn't work. (compiler error C3699). Do I replace & with ^ ?
>
> The equivalent code would be
>
> ref class MyClass {
>     public:
>     MyClass(void);
>     ~MyClass(void);
>     MyClass(MyClass%);
>    MyClass% operator=(const MyClass%);
> };
>
> Think of the operator equivalence for definitions like this:
> &    =    %
> *    =    ^
>
> Dereferencing a ^ is still done with *, though.
Ioannis Vranos - 24 May 2005 18:39 GMT
> To follow up on the "copy constructor clarification thread"...
>
[quoted text clipped - 20 lines]
> class's properties? The copy constructor doesn't have this problem since its
> parameter isn't const.

As far as I can understand the compiler is completely broken on the implementation of
properties.

Consider the following code:

ref class MyClass
{
    int someProperty;

    public:

       property int SomeProperty
       {
            int get() { return someProperty; }
            void set(int newValue) { someProperty = newValue; }
       }

        MyClass %operator=(const MyClass %m)
        {
            someProperty= m.someProperty;

            return *this;
        }
};

int main()
{
    using System::Console;

    MyClass obj1, obj2;

    obj1.SomeProperty= 6;

    obj2= obj1;

    Console::WriteLine(obj2.SomeProperty);
}

someProperty is private, so how does m.someProperty gets accessed? Try to change it to
SomeProperty.
Ioannis Vranos - 29 May 2005 04:47 GMT
> As far as I can understand the compiler is completely broken on the
> implementation of properties.
>
> someProperty is private, so how does m.someProperty gets accessed? Try
> to change it to SomeProperty.

My mistake, the compiler is OK on this.
Tamas Demjen - 24 May 2005 23:23 GMT
> What's the correct syntax for an assignment operator and the copying of a
> class's properties? The copy constructor doesn't have this problem since its
> parameter isn't const.

The proper syntax is

MyClass% MyClass::operator=(MyClass% m)

There's no const support in .NET, unfortunately. This is quite an
inconvenience, but you have to live with that when you program for .NET.
I hope Microsoft will implement const in CLR as soon as possible. I can
hardly imagine that we have to go back to prehistoric ages when there
was no const, but apperently that's the situation. :-(

By the way, the "prop = m.type" line should work fine, even though
"type" is private. A class must have access to its own private members,
so it has to work. That can't be the problem. Your problem is with the
const keyword, I think.

Tom
Kapil Khosla [MSFT] - 25 May 2005 19:06 GMT
Signature

Kapil Khosla, Visual C++ Team
This posting is provided AS IS with no warranties, and confers no rights

> > What's the correct syntax for an assignment operator and the copying of a
> > class's properties? The copy constructor doesn't have this problem since its
[quoted text clipped - 16 lines]
>
> Tom

You are right, const is not supported by the runtime but the managed C++
compiler supports it in a semi partial manner. For example you can do
void foo(const int) {}
but not
void foo(int) const {}

which is legal ISO C++. The restriction is more from the runtime rather than
the compiler side. The same error will occur if you call a member function on
a const object.

Short answer, please remove the const from the assignment operator syntax
and regarding the support for properties, they are fully supported in the
compiler. If you find anything which doesnt work please let me know and we
shall look into it right away.

Thanks,
Kapil
Tamas Demjen - 25 May 2005 19:20 GMT
Thanks Kapil.

If there's no support for const methods, you virtually can't use const
function arguments, except for native types. Const correctness is a very
important quality control aspect. I hope Microsoft will consider
implementing it in the future. I understanding that it's not going to
happen in VS 2005.

Tom

Rate this thread:







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.