473,387 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

assignment operator syntax

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


Nov 17 '05 #1
5 1434
Rob wrote:
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.

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.
Nov 17 '05 #2
Rob wrote:
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
Nov 17 '05 #3

--
Kapil Khosla, Visual C++ Team
This posting is provided AS IS with no warranties, and confers no rights
"Tamas Demjen" wrote:
Rob wrote:
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


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
Nov 17 '05 #4
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
Nov 17 '05 #5
Ioannis Vranos wrote:
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.
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
7
by: Andy Lomax | last post by:
The C99 standard contains various statements like this one (in this case, 6.5.16, assignment operator): >If an attempt is made to modify >the result of an assignment operator or to access it...
5
by: Peter Seaman | last post by:
I do recognise a number of very appealing features of C#, in particular the strong typing and the need in some contexts to be very explicit regarding your intention. But the most common operator -...
77
by: berns | last post by:
Hi All, A coworker and I have been debating the 'correct' expectation of evaluation for the phrase a = b = c. Two different versions of GCC ended up compiling this as b = c; a = b and the other...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
12
by: subramanian100in | last post by:
For a class Test, we write the assignment operator as Test & Test::operator=(const Test & rhs); instead of const Test & Test::operator=(const Test & rhs); that is, the return type of...
1
by: subramanian100in | last post by:
Consider the following: int x; int y; int z; (x+y) = z; For this statement, I get the following error with g++ compiler: error: non-lvalue in assignment Suppose I have a class Test and x,...
5
by: arashmath | last post by:
Hi, How can i use default assignment operator (member-by-member assignment) after overloading this operator? I want to access orginal version of assigment operator (=), but i don't know the syntax...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.