473,568 Members | 2,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::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:

"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 1451
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::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.

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


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
3208
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 there are no assignment expressions in Python, I have to use a temp var. That's a little more messy, but bearable:
7
1959
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 after the next sequence point, the >behavior is undefined. What does this actually mean? Can anyone give me a code example that leads to undefined...
5
2041
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 - assignment -seems to me a right source of confusion and mistakes because it has two very different meanings. Why not have one syntax for value...
77
4680
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 ended up compiling it as a = c; b = c. (Both with no optimizations enabled). How did we notice you may ask? Well, in our case 'b' was a memory...
5
2281
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 my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler...
9
3486
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 allowing the assignment operator (=) to be overloaded. One particular use for this would be to implement "lazy evaluation". For example it would allow...
12
438
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 operator=( ) is just a reference, not a reference to const object.
1
2534
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, y, z are objects of type Test.
5
1768
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 that imply compiler to use default version and not to use overload version.
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.