So, for our nbits math (to reflect hardware registers, buffers, etc that
have n bit width), we should use
AddInt3(&a, a, b);
instead of
a += b;
when a and/or b is a int3 that is a three-bit value?
Just teasing. 
I know what you mean, and agree. There are very good reasons to sometimes
use operator overload. But it should be used sparingly and with good
thoughs.
It’s very easy to have overload overload (pun intended) and start using
overloads here, there and everywhere, because it seems like a “neat idea”.
But at the same time, it really IS brilliant to be able to do simple math
with integers of varying width in our simulator. And it would be MUCH harder
to do if we didn’t use overloaded operators in C++. In fact, it’s probably
one of the best features in C++ when used to write a simulator for a chip.
(Another alternative would of course to write a VHDL compiler, and use VHDL
to describe registers and other parts as n bits wide, and then get the
correct result).
Complex number math and String assignment with = or concatenation with + are
some other examples of reasonable use of operator overloads, but it’s easy
for a novice to think that the simple operator sign is actually just a
single instruction (or a few instructions), when in fact it’s copying, say,
200 bytes of data.
C++ is a good tool when used correctly, but same as with a chainsaw, it’s
got enough power to make it very dangerous if used incorrectly.
I’m by no means saying that writing drivers in C++ is a particularly good
idea, but sometimes I actually think to myself “This would be so much easier
if we could just inherit the original structure and override the three
functions that are different”. This is particularly when (like now) I’m
writing a piece of code that is for a variation on a previous chip, just
slightly different in a few places. You end up with lots of “if (x)
something; else someother;” in the code. With classes and inheritance, it
would be fairly trivial to implement a different member function for
particular functionalities that are different, and keeping the other bits
the same. I’m essentially doing this anyway, but we’re using function
pointers, so I’m calling InitTypeB, which calls InitTypeA and then
overwrites a few of the function pointers… This type of thing would happen
automatically in C++…
But at the same time, things that happen automatically is dangerous. If it’s
written explicitly in the code, you know it’s happening. If it’s not written
in the code, you need to understand more about what’s going on in the system
to understand what’s actually happening. That’s a big danger with C++.
–
Mats
-----Original Message-----
From: Roddy, Mark [mailto:xxxxx@stratus.com]
Sent: Thursday, February 12, 2004 5:38 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A guide to writing device drivers in c++
Operator overload is hardly ever justified as it makes code
comprehension
very difficult and consequently causes unintended consequence
bugs as a
software product evolves over time. You could not have picked a better
example of why NOT to use C++.
=====================
Mark Roddy
From: Moreira, Alberto [mailto:xxxxx@compuware.com]
Sent: Thursday, February 12, 2004 11:25 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A guide to writing device drivers in c++
It takes to learn that operator semantics, just like function
semantics, now belong *inside their class*, they’re no longer
global. So, if
I say c=a+b, the result I get depends on the definition of
the operator “+”.
If a, b and c are strings, for example, “+” may mean
concatenation. Welcome
to object orientation !
If you want the definition of |=, just look at the
declaration of
the respective class in the .h file.
Alberto.
-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com]On Behalf Of Peter Scott
Sent: Thursday, February 12, 2004 11:20 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A guide to writing device
drivers in
c++
Great, that must be fun code to read through. X |= 0x04
equals 42 for all values of X being prime
Peter Scott
xxxxx@KernelDrivers.com
www.KernelDrivers.com
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
Moreira, Alberto
Sent: Thursday, February 12, 2004 7:54 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A guide to writing device
drivers in
c++
In C++, the scribble “|=” stands for an
operator. So, I can
overload it inside my class and get it to be whatever I need
it to be.
Alberto.
-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com]On Behalf Of Jamey Kirby
Sent: Wednesday, February 11, 2004 6:27 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A guide to writing device
drivers in
c++
So, it is better to call a class rather
than X |=
0x01?
Maybe I am getting to old; I can even
figure out how
to use 1/3rd of my cell phones features.
From:
xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com]
On Behalf Of Moreira, Alberto
Sent: Wednesday, February 11, 2004 12:12 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] A guide to writing device
drivers in c++
Depends on your granularity, no ? If
you need a lot
of bit-level functionality, maybe set things up in your way
makes sense.
Alberto.
-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com]On Behalf Of Calvin Guan
Sent: Wednesday, February 11,
2004 3:05 PM
To: Windows System Software
Devs Interest
List
Subject: RE: [ntdev] A guide to writing
device drivers in c++
> You can also take advantage of
inheritance, for example,
>
> class IRP { … } ;
> class pnpIRP : IRP { … };
> class startDeviceIRP : pnpIRP
{ … };
> class stopDeviceIRP : pnpIRP
{ … };
class BIT {…};
class BYTE:BIT {…};
class U16:BYTE {…};
class U32:U16 {…};
-
Calvin Guan Software Engineer
ATI Technologies Inc. www.ati.com
Questions? First check the
Kernel Driver FAQ
at http://www.osronline.com/article.cfm?id=256
You are currently subscribed to
ntdev as:
xxxxx@compuware.com
To unsubscribe send a blank email to
xxxxx@lists.osr.com
Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
You are currently subscribed to ntdev as:
xxxxx@storagecraft.com
To unsubscribe send a blank email to
xxxxx@lists.osr.com
Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
You are currently subscribed to ntdev as:
xxxxx@compuware.com
To unsubscribe send a blank email to
xxxxx@lists.osr.com
The contents of this e-mail are intended for the
named addressee only. It contains information that may be
confidential.
Unless you are the named addressee or an authorized designee,
you may not
copy or use it, or disclose it to anyone else. If you
received it in error
please notify us immediately and then destroy it.
Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
You are currently subscribed to ntdev as:
xxxxx@kerneldrivers.com
To unsubscribe send a blank email to
xxxxx@lists.osr.com
Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
You are currently subscribed to ntdev as:
xxxxx@compuware.com
To unsubscribe send a blank email to
xxxxx@lists.osr.com
The contents of this e-mail are intended for the named
addressee only. It contains information that may be
confidential. Unless you
are the named addressee or an authorized designee, you may
not copy or use
it, or disclose it to anyone else. If you received it in
error please notify
us immediately and then destroy it.
Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
You are currently subscribed to ntdev as: xxxxx@stratus.com
To unsubscribe send a blank email to
xxxxx@lists.osr.com
The contents of this e-mail are intended for the named addressee
only. It contains information that may be confidential.
Unless you are the
named addressee or an authorized designee, you may not copy
or use it, or
disclose it to anyone else. If you received it in error
please notify us
immediately and then destroy it.
Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
You are currently subscribed to ntdev as: xxxxx@3dlabs.com
To unsubscribe send a blank email to xxxxx@lists.osr.com