473,387 Members | 1,574 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.

About some understanding from a book

Hello!
I think that this text is complete enough so you can give me an explanation
what they mean with it.

I wonder if somebody understand what this means. It says the following :
"You can't overload assignment operator, such as +=, but these operator use
their simple counterpart, such as +, so you don't have to worry about that.
Overloading + means that += will function as expected.
The = operator is included in this -- it makes little sense to overload
this operator, since it has such a fundamental usage. This operator,
however,
is related to user-defined conversion operator, which you'll look at in the
next section."
//Tony
Jun 27 '08 #1
8 1205
On Thu, 12 Jun 2008 10:24:44 -0700, Tony Johansson
<jo*****************@telia.comwrote:
I think that this text is complete enough so you can give me an
explanation
what they mean with it.
Well, the words seem reasonably self-evident. Basically, they are calling
out the operators that can't be overloaded, and pointing out why. So,
what exactly about it don't you understand?
Jun 27 '08 #2
On Jun 12, 1:24*pm, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
Hello!
I think that this text is complete enough so you can give me an explanation
what they mean with it.

I wonder if somebody understand what this means. It says the following :
"You can't overload assignment operator, such as +=, but these operator use
their simple counterpart, such as +, so you don't have to worry about that..
Overloading + means that += will function as expected.
The = operator is included in this -- it makes little sense to overload
this operator, since it has such a fundamental usage. This operator,
however,
is related to user-defined conversion operator, which you'll look at in the
next section."

//Tony
Hi,

What is what you do not understand?
It's saying that you cannot overload += but you can get the same
effect by overloading + (which makes a lot of sense) imagine how weird
it would be if :
a+= b
would be different than
a= a+ b
Jun 27 '08 #3
Hi,

While we are talking about operator overload, could someone explain to
me how are you supposed to use the “true” and “false” unary operator
overload?

public static bool operator true(SomeClass x)
{
// …
}

public static bool operator false(SomeClass x)
{
// …
}

It’s probably very obvious and I am sure goggling it will shed some
light about how this supposed to work but I figure I post this here
since is somewhat related with the OP question.

Thanks.
Jun 27 '08 #4
On Thu, 12 Jun 2008 11:41:20 -0700, <qg**********@mailinator.comwrote:
While we are talking about operator overload, could someone explain to
me how are you supposed to use the “true” and “false” unary operator
overload?
If I read the spec correctly, these operators are used when you have a
type that semantically can be thought of as "true" or "false" (that is, it
essentially has some boolean state). You pass in an instance of the type,
and the "true" operator returns the bool "true" if the type is in the
"true" state. The "false" would be the inverse (you're required to define
both, but I expect you'd generally just have one call the other and invert
the result. Any other design could start to get confusing.

You can take advantage of this to allow boolean operations on such a type,
even though you can't overload && and ||. By overloading & and |, as well
as "true" and "false", you allow the compiler to infer the correct && and
|| behavior. Paraphrasing the C# spec, where you have variables "x" and
"y" of a type T as described above, the expression "x && y" would be
evaluated as "T.false(x) ? x : T.&(x, y)" and the expression "x || y"
would be evaluated as "T.true(x) ? x : T.|(x, y)".

Since the type T would have the "true" and "false" operators overloaded,
the compiler can take the results from the & and | operators and convert
that from an instance of the type T to values of the actual "bool" type.
So the expressions above, which actually return a value of type T, could
then be run through the "true" or "false" operator, as appropriate to
whatever the final expression really is (I assume the compiler would use
the "true" operator for a plain expression and the "false" operator if
you've also used the negation operation !, but of course it could just
always use "true" and then perform whatever necessary logic on that
boolean after).

I don't know for sure why the language is like this. However, I can see
how designing it like this ensures that there is logical consistency
between the &&/|| operators and the &/| operators, any time the type
author decides they want their type to support both (it won't always make
sense for a type to support both, but in some cases it does).
It’s probably very obvious and I am sure goggling it will shed some
light about how this supposed to work but I figure I post this here
since is somewhat related with the OP question.
I don't think it's necessarily obvious at all, and for sure the MSDN docs
could do a much better job of elaboration. However, I definitely would
recommend that if these are questions you're curious about, you download
the C# spec, as it does generally answer these kinds of questions.

Of course, if you hadn't asked it, I never would have bothered to look at
the spec myself, and of course assuming my interpretation is useful, this
newsgroup would never have benefitted from having the question answer
posted here. So maybe it's better to just ask the question. :)

Pete
Jun 27 '08 #5
If I read the spec correctly, these operators are used when you have a
type that semantically can be thought of as "true" or "false" (that is, it
essentially has some boolean state). You pass in an instance of the type,
and the "true" operator returns the bool "true" if the type is in the
"true" state. The "false" would be the inverse (you're required to define
both, but I expect you'd generally just have one call the other and invert
the result. Any other design could start to get confusing.
I just took a look at the spec out of interest, and it appears that the
"true" operator does not necessarily have to return the inverse of the
"false" operator. Section 11.4.2 of the spec shows a 3-valued type (database
boolean, which can be true, false or null) where operator true means
"definitely true" and operator false means "definitely false". Thus if the
value is null both operators return false. (I don't claim to understand the
logic behind this, but I have hopes that if I read it through a few more
times it will start to make sense!)

Chris Jobson
Jun 27 '08 #6
On Thu, 12 Jun 2008 14:27:29 -0700, Chris Jobson
<ch**********@btinternet.comwrote:
>[...] (you're required to define
both, but I expect you'd generally just have one call the other and
invert
the result. Any other design could start to get confusing.

I just took a look at the spec out of interest, and it appears that the
"true" operator does not necessarily have to return the inverse of the
"false" operator.
Sure, that's what I implied, by writing "generally" rather than "this is
always how it'd be done" (I forgot to close my paren, but whatever :) ).
My point being that doing it in some other way could be confusing. That
said, the DB example you give seems reasonable (I didn't notice it when I
was looking at the spec). BUT! I by "reasonable" I just mean
"justified". I still think the design itself would be more confusing,
even in that example.
Section 11.4.2 of the spec shows a 3-valued type (database
boolean, which can be true, false or null) where operator true means
"definitely true" and operator false means "definitely false". Thus if
the
value is null both operators return false. (I don't claim to understand
the
logic behind this, but I have hopes that if I read it through a few more
times it will start to make sense!)
If I may:

The point would be that code that cared about "definitely true" would
check for "true"-ness, while code that cared about "definitely false"
would check for "false"-ness. I would guess that in most cases, it's not
so much that you'd check for "false" from both operators, but that you'd
have different code keying off of "true" results from either.

That said, that brings me back to my previous supposition about how the
compiler would actually use those operators. In particular, in my mind
you'd write code like:

if (myClass)
{
// something we do only when it's "definitely true"
}

if (!myClass)
{
// something we do only when it's "definitely false"
}

But this would rely on the compiler _always_ using the ! operator as an
indicator to use the "false" operator instead of the "true" operator. I'd
have to go back and look at the spec, but unless it _specifically_ say
that the compiler is required to do this, then it's entirely possible a C#
compiler could just ignore the "false" operator and just always invert the
result of the "true" operator when faced with the ! operator.

In that scenario, the DB example would break, unless the compiler is
mandated to behave in a specific way.

Maybe if I get interested enough, later I'll look at what the spec says
about that. :)

Pete
Jun 27 '08 #7
>I just took a look at the spec out of interest, and it appears that the
>"true" operator does not necessarily have to return the inverse of the
"false" operator.

Sure, that's what I implied, by writing "generally" rather than "this is
always how it'd be done" (I forgot to close my paren, but whatever :) ).
Fair enough.

....
That said, that brings me back to my previous supposition about how the
compiler would actually use those operators. In particular, in my mind
you'd write code like:

if (myClass)
{
// something we do only when it's "definitely true"
}

if (!myClass)
{
// something we do only when it's "definitely false"
}

But this would rely on the compiler _always_ using the ! operator as an
indicator to use the "false" operator instead of the "true" operator. I'd
have to go back and look at the spec, but unless it _specifically_ say
that the compiler is required to do this, then it's entirely possible a C#
compiler could just ignore the "false" operator and just always invert the
result of the "true" operator when faced with the ! operator.
I've had another look at the spec - I ought to have better things to do :) -
and AIUI when evaluating
myClass
as a boolean it first looks for an implicit conversion, and if there isn't
one it uses the "true" operator. Thus when evaluating
if (!myClass)
in the absence of an implicit conversion it _must_ invert the result of the
"true" operator.
In that scenario, the DB example would break, unless the compiler is
mandated to behave in a specific way.
The DB example solves the problem by defining its own version of the "!"
operator, so when evaluating
if (!myDBClass)
there's no need to evaluate myDBClass as a boolean and so the rules above
don't come into play.

Interestingly it appears from the spec that the _only_ time the "false"
operator is used is when evaluating
myClass1 && myClass2

Chris
Jun 27 '08 #8
On Fri, 13 Jun 2008 09:51:32 -0700, Chris Jobson
<ch**********@btinternet.comwrote:
[...]
I've had another look at the spec - I ought to have better things to do
:) -
and AIUI when evaluating
myClass
as a boolean it first looks for an implicit conversion, and if there
isn't
one it uses the "true" operator. Thus when evaluating
if (!myClass)
in the absence of an implicit conversion it _must_ invert the result of
the
"true" operator.
Interesting. That seems dangerous.
>In that scenario, the DB example would break, unless the compiler is
mandated to behave in a specific way.

The DB example solves the problem by defining its own version of the "!"
operator, so when evaluating
if (!myDBClass)
there's no need to evaluate myDBClass as a boolean and so the rules above
don't come into play.

Interestingly it appears from the spec that the _only_ time the "false"
operator is used is when evaluating
myClass1 && myClass2
And IMHO that would be a very bad time for the "false" operator to not
just be the opposite of the "true" operator. In particular, if you can
have both "false" and "true" return "false", maybe there's also some
scenario where you'd have both of them return "true". In which case
"myClass1 && myClass2" would be false, even though the expression could be
logically thought of as true.

I admit, the likelihood of it coming up seems exceedingly small. But it's
an odd gap in the spec for a language that otherwise seems to go to great
lengths to limit the opportunities to shoot oneself in the foot to those
where it's relatively obvious you're doing so. :)

Anyway, thanks for checking that out.

Pete
Jun 27 '08 #9

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

Similar topics

1
by: William Ryan | last post by:
Well, that depends. http://www.amazon.com/exec/obidos/tg/detail/- /0672320681/qid=1056861878/sr=1-1/ref=sr_1_1/002-7953039- 7491213?v=glance&s=books Is a must read IMHO for any ASP.NET...
1
by: Tony Johansson | last post by:
Hello! I'm reading a book about C++ and there is something that I don't understand so I ask you. I have marked the section from the book that is of intertest by tagging it with BOOK START HERE...
3
by: Ron Vecchi | last post by:
I am creating my own MainMenu control similar to VS.Net. (yes I know theirs some out their). I have predominatly been a web developer for about 5 years and am playing with Windows forms. So this...
11
by: John Salerno | last post by:
I'm thinking about reading Beginning C# Objects: From Concepts to Code because I still don't have a great grasp of objects, but I wonder if C# 2.0 will change things enough that a lot of what's in...
3
by: JEHalm | last post by:
I am reading a book (Beginning Access 2002 VBA) and I am having a problem understanding the use of recordsets. Can anyone explain the value of creating recordsets or how they are practically...
1
by: hema chandran | last post by:
Hello sir I am fresher in DOTNET sir, so plz help me referents book Thank u sir This is my mail i.d hemudotnet@yahoo.com
1
by: Tony Lawrence | last post by:
Probably a lot of you here are already old hands at Ajax, but I haven't started doing this yet. Part of the reason was that I really didn't understand where Ajax would be useful to my own site,...
75
by: Steven T. Hatton | last post by:
No, this is not a troll, and I am not promoting Java, C-flat, D, APL, Bash, Mathematica, SML, or LISP. A college teacher recently posted to this newsgroup regarding her observation that there has...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
56
by: nembo kid | last post by:
What do you think about the following book: C How to Program, 5/E (Harvey & Paul) Deitel & Associates, Inc. <http://www.pearsonhighered.com/educator/academic/product/0,3110,0132404168,00.html>...
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: 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
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
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,...
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.