473,386 Members | 1,835 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,386 software developers and data experts.

assignment operator

In C, we can compare(ie apply the equality operators == and != to)
pointers to elements of the same array only. We cannot compare the
pointers to different objects of the same type. In C++, consider the
operator=.

For a class X,

X& operator=(const X &ref)
{
if (this != &ref)
{
...
}

return *this;
}

Here we are comparing the pointers to objects of the same type X. But
they need not belong to the same array. Kindly explain how this is
allowed.

Apr 19 '07 #1
9 3260
su**************@yahoo.com wrote:
In C, we can compare(ie apply the equality operators == and != to)
pointers to elements of the same array only.
OK, I didn't know that there was a restriction on the equality ops.
IIRC, there was only the restriction for comparison ops (< <= >=).
We cannot compare the
pointers to different objects of the same type. In C++, consider the
operator=.

For a class X,

X& operator=(const X &ref)
{
if (this != &ref)
{
...
}

return *this;
}

Here we are comparing the pointers to objects of the same type X. But
they need not belong to the same array. Kindly explain how this is
allowed.
Pointers are allowed to be compared for equality regardless of the
objects' belonging to the same array. The restriction exists for the
use of comparison operators (< <= >=). Those can only be used to
_compare_ pointers to elements of the same array or members of the
same object.

Consider this: you compare pointers to 0 (or to a null pointer) all
the time, right? A null pointer is not a pointer to any object, yet
you have no problem comparing a pointer to any other object to it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 19 '07 #2
On Apr 19, 3:41 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
In C, we can compare(ie apply the equality operators == and != to)
pointers to elements of the same array only.
Since when? This restriction isn't present in C99, and it
certainly wasn't present in earlier versions. As far as I know,
C and C++ use exactly the same rules with regards to pointer
comparison.
We cannot compare the
pointers to different objects of the same type. In C++, consider the
operator=.
For a class X,
X& operator=(const X &ref)
{
if (this != &ref)
This is a red flag: if a test for self assignment is necessary,
9 times out of 10, the assignment operator is broken.
{
...
}
return *this;
}
Here we are comparing the pointers to objects of the same type X. But
they need not belong to the same array. Kindly explain how this is
allowed.
Kindly explain why you think it's not allowed.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Apr 19 '07 #3
On Apr 19, 10:06 am, James Kanze <james.ka...@gmail.comwrote:
<snip>
X& operator=(const X &ref)
{
if (this != &ref)

This is a red flag: if a test for self assignment is necessary,
9 times out of 10, the assignment operator is broken.
I have a couple of problems with this "rule of thumb":

1) That it is a bit of an overstatement. It would only be broken in
the sense of
exception-safety. Those concerns are not universal.

2) You may use the comparison for performance reasons, not for
correct
behavior.

Apr 20 '07 #4
On Apr 20, 3:53 am, xperthands <xpertha...@gmail.comwrote:
On Apr 19, 10:06 am, James Kanze <james.ka...@gmail.comwrote:
<snip>
X& operator=(const X &ref)
{
if (this != &ref)
This is a red flag: if a test for self assignment is necessary,
9 times out of 10, the assignment operator is broken.
I have a couple of problems with this "rule of thumb":

1) That it is a bit of an overstatement. It would only be broken in
the sense of
exception-safety. Those concerns are not universal.
In what sense: that the concern for writing correct code is not
universal? If the class consists of only primitive types, which
cannot throw, then there's no need for the test; some would
argue that there's no need for the user defined operator= to
begin with.
2) You may use the comparison for performance reasons, not for
correct
behavior.
Bullshit. It could only improve performance if most assigns
were self assigns; if not assigning to self, it slows things
down (slightly).

The fact remains that when looking at foreign code, a test for
self assignment is a red flag that the operator= is broken.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 20 '07 #5
On Apr 20, 12:41 am, James Kanze <james.ka...@gmail.comwrote:
On Apr 20, 3:53 am, xperthands <xpertha...@gmail.comwrote:
1) That it is a bit of an overstatement. It would only be broken in
the sense of
exception-safety. Those concerns are not universal.

In what sense: that the concern for writing correct code is not
universal? If the class consists of only primitive types, which
cannot throw, then there's no need for the test; some would
argue that there's no need for the user defined operator= to
begin with.
Your overstating again. Your class may be composed of non-
primitives that also have nothrow guarantees. primitives are not
the only types that fit that.
Bullshit.
I know you're a moderator, but such rude language is
offensive. I should think that moderators would be held
to a higher standard.
It could only improve performance if most assigns
were self assigns; if not assigning to self, it slows things
down (slightly).
Again, your knowledge of the use of the type can make
the world of difference. Which was indeed my point.
The fact remains that when looking at foreign code, a test for
self assignment is a red flag that the operator= is broken.
I still disagree with that it is necessarily broken. I also use
that as a sign to look more deeply at the class and the
assignment operator, but when doing a code review, I try
not to jump to conclusions. That can introduce
unnecessary tension into the process. That tenson can
lead to problems in your review process.

Apr 22 '07 #6
* xperthands:
On Apr 20, 12:41 am, James Kanze <james.ka...@gmail.comwrote:
>Bullshit.

I know you're a moderator, but such rude language is
offensive. I should think that moderators would be held
to a higher standard.
I disagree with James on the technical issue, that the statement to
which he replied was bullshit (that doesn't necessarily mean I agree
with you, but I certainly disagree with James here), but, /if/ I'd
agreed with James' reasoning, I may have used the same word.

We shouldn't be afraid to use what we think are accurate labels, short
concise language, just because someone might be offended.

It would be quite another matter to characterize a person, as opposed to
a statement, that way.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 22 '07 #7
On Apr 22, 6:03 am, xperthands <xpertha...@gmail.comwrote:
On Apr 20, 12:41 am, James Kanze <james.ka...@gmail.comwrote:
On Apr 20, 3:53 am, xperthands <xpertha...@gmail.comwrote:
1) That it is a bit of an overstatement. It would only be broken in
the sense of
exception-safety. Those concerns are not universal.
In what sense: that the concern for writing correct code is not
universal? If the class consists of only primitive types, which
cannot throw, then there's no need for the test; some would
argue that there's no need for the user defined operator= to
begin with.
Your overstating again. Your class may be composed of non-
primitives that also have nothrow guarantees. primitives are not
the only types that fit that.
I'm still waiting for an example of a class where such a test
would be appropriate. As I said, it's a red flag---when I see
it, I get very suspicious. To date, my suspicions have always
been confirmed.
Bullshit.
I know you're a moderator, but such rude language is
offensive. I should think that moderators would be held
to a higher standard.
This isn't a moderated group. And the comment applies to your
statement, not to you. While I might have used a more
politically correct word in a moderated group, I wouldn't
participate in a group where I couldn't say the equivalent in
some way or another. I find it essential to be able to
criticize statements. (Fundamentally, except for the level of
language, what is the difference between "bullshit", and what
you said about my statements. Both are, IMHO, comments about
what was said, and not the person.)

And I'll admit that I'm letting myself go a bit. admit In a
newgroup, where you don't know the sensibilities of your
interlocutors, it's probably better to err on the side of
caution, and to avoid such language. Sometimes, however, it
feels good to just let off steam, especially after having been
bottled up for so long.
It could only improve performance if most assigns
were self assigns; if not assigning to self, it slows things
down (slightly).
Again, your knowledge of the use of the type can make
the world of difference. Which was indeed my point.
The fact remains that when looking at foreign code, a test for
self assignment is a red flag that the operator= is broken.
I still disagree with that it is necessarily broken.
Saying that something is a red flag doesn't mean that it is
necessarily broken. It does say that most of the time the idiom
is used, it is mistakenly, and that when I see it, I start
checking, because I've seen it used so often mistakenly.

And as I said, I've never seen a case where it was necessary, if
the operator was otherwise correct.
I also use that as a sign to look more deeply at the class and
the assignment operator, but when doing a code review, I try
not to jump to conclusions. That can introduce unnecessary
tension into the process. That tension can lead to problems in
your review process.
OK. Then we're really in agreement, just expressing ourselves
differently. At least in the English I know, a "red flag" means
a signal to look closer. Just as obviously, in code review, I
will adjust the tone of my comments to the people present---some
people are more sensitive than others, and my presentation will
take that into account. And I've been in code reviews where I
could say that something was bullshit; it all depends on the
personality of the person involved. (Depending on the people
involved and the context, informal language can sometimes serve
to break down tension, as well as to create it.)

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 22 '07 #8
On Apr 20, 5:06 am, James Kanze <james.ka...@gmail.comwrote:
>
Since when? This restriction isn't present in C99, and it
certainly wasn't present in earlier versions. As far as I know,
C and C++ use exactly the same rules with regards to pointer
comparison.
The rules are the same for equality comparisons (namely, it is
always allowed). The rules for relational comparisons are different;
in C it is undefined behaviour if the pointers do not point to parts
of the same object (or one past the end), but in C++ it is
unspecified.

Apr 22 '07 #9
In article <11*********************@q75g2000hsh.googlegroups. com>,
ol*****@inspire.net.nz says...
On Apr 20, 5:06 am, James Kanze <james.ka...@gmail.comwrote:

Since when? This restriction isn't present in C99, and it
certainly wasn't present in earlier versions. As far as I know,
C and C++ use exactly the same rules with regards to pointer
comparison.

The rules are the same for equality comparisons (namely, it is
always allowed). The rules for relational comparisons are different;
in C it is undefined behaviour if the pointers do not point to parts
of the same object (or one past the end), but in C++ it is
unspecified.
It's also worth mentioning that std::less gives (loosely) specified
results even when the operators don't.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Apr 23 '07 #10

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

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
5
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
9
by: Rick N. Backer | last post by:
I have an abstract base class that has char* members. Is an assignment operator necessary for this abstract base class? Why or why not? Thanks in advance. Ken Wilson Amer. Dlx. Tele,...
9
by: Matthew Polder | last post by:
Hi, When a class Apple is written and the assignment operator is not explicitly declared, the operator Apple& operator=(const Apple&) is created by the compiler. Is there any difference...
1
by: Jon Slaughter | last post by:
I have a chain of classes(i.e., a series of classes each containing an array of the next class). Each class has array like access. struct Myclass1 { vector(Myclass2) _Myclass2; Myclass2&...
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
11
by: anongroupaccount | last post by:
What measures should be taken to avoid this sort of thing? class Base { }; class Derived1 : public Base { private: int i, j, k;
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: George2 | last post by:
Hello everyone, I am wondering the default implementation of assignment operator (e.g. when we do not implement assignment operator in user defined class, what will be returned? temporary...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...
0
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...
0
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,...
0
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...

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.