assignment operator syntax

G

Guest

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::blush:perator=(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
 
I

Ioannis Vranos

Rob said:
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::blush:perator=(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.


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.
 
T

Tamas Demjen

Rob said:
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::blush:perator=(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
 
G

Guest

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


Tamas Demjen said:
The proper syntax is

MyClass% MyClass::blush:perator=(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

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
 
T

Tamas Demjen

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
 
I

Ioannis Vranos

Ioannis said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top