assignment operator syntax

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob

    #1

    assignment operator syntax

    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::operat or=(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:
    [color=blue]
    >
    > "Rob" wrote:
    >[color=green]
    > > 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 ^ ?[/color]
    >
    > 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.
    >[/color]

  • Ioannis Vranos

    #2
    Re: assignment operator syntax

    Rob wrote:
    [color=blue]
    > 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::operat or=(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.[/color]


    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=(cons t MyClass %m)
    {
    someProperty= m.someProperty;

    return *this;
    }
    };


    int main()
    {
    using System::Console ;

    MyClass obj1, obj2;

    obj1.SomeProper ty= 6;

    obj2= obj1;

    Console::WriteL ine(obj2.SomePr operty);
    }



    someProperty is private, so how does m.someProperty gets accessed? Try to change it to
    SomeProperty.

    Comment

    • Tamas Demjen

      #3
      Re: assignment operator syntax

      Rob wrote:[color=blue]
      > 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.[/color]

      The proper syntax is

      MyClass% MyClass::operat or=(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

      Comment

      • Kapil Khosla [MSFT]

        #4
        Re: assignment operator syntax


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


        "Tamas Demjen" wrote:
        [color=blue]
        > Rob wrote:[color=green]
        > > 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.[/color]
        >
        > The proper syntax is
        >
        > MyClass% MyClass::operat or=(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
        >[/color]

        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

        Comment

        • Tamas Demjen

          #5
          Re: assignment operator syntax

          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

          Comment

          • Ioannis Vranos

            #6
            Re: assignment operator syntax

            Ioannis Vranos wrote:
            [color=blue]
            > 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.[/color]


            My mistake, the compiler is OK on this.

            Comment

            Working...