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