473,657 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

typedef vs inheritance

I found in C++ a type is typedef several times and alot of time, I
need to dig further to see the original type.

like LPDWORD - it's a strange type, but when i dig further, i see it's
a (DWORD *)

===========
typedef DWORD near *PDWORD;
typedef DWORD far *LPDWORD;

===========
i feel that creating new type in C++ by using "typedef" is similar to
inheritance in Java -

where alot of time I need to dig further to found the "PARENT" type of
an object hierarchy.
Jun 27 '08 #1
8 3738
Carmen Sei wrote:
I found in C++ a type is typedef several times and alot of time, I
need to dig further to see the original type.
You or Eric?
like LPDWORD - it's a strange type, but when i dig further, i see it's
a (DWORD *)
So what's a DWORD?
===========
typedef DWORD near *PDWORD;
typedef DWORD far *LPDWORD;

===========
i feel that creating new type in C++ by using "typedef" is similar to
inheritance in Java -
A typedef is an alias, not a new type.

--
Ian Collins.
Jun 27 '08 #2
Sam
Carmen Sei writes:
like LPDWORD - it's a strange type, but when i dig further, i see it's
a (DWORD *)

===========
typedef DWORD near *PDWORD;
typedef DWORD far *LPDWORD;

===========
i feel that creating new type in C++ by using "typedef" is similar to
inheritance in Java -
A typedef is an alias. In the above example, "PDWORD" can be used anywhere
"DWORD near *" is valid, and vice versa. You can pass a "DWORD near *" as a
function parameter whose type is defined as "PDWORD" (and the other way
around too). In Java, the equivalent does not hold true. You cannot pass a
superclass as a function parameter whose type is a subclass. Also, as an
aside, there is no such thing as "near" and "far" in standard C++, that's
just a perversion that's infesting Microsoft Windows since the MS-DOS days.

There is no direct Java equivalent to a C++ typedef.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBIBqzgx9p 3GYHlUOIRAkwtAJ 0dqQwpWGwfoa0Hv YWb/f1xOOufSACfSjvS
jVruaMstl07b3IN aUNDjlWY=
=Vzcr
-----END PGP SIGNATURE-----

Jun 27 '08 #3
On Apr 16, 6:22 pm, Carmen Sei <fatwallet...@y ahoo.comwrote:
I found in C++ a type is typedef several times and alot of time, I
need to dig further to see the original type.

like LPDWORD - it's a strange type, but when i dig further, i see it's
a (DWORD *)

===========
typedef DWORD near *PDWORD;
typedef DWORD far *LPDWORD;

===========
i feel that creating new type in C++ by using "typedef" is similar to
inheritance in Java -

where alot of time I need to dig further to found the "PARENT" type of
an object hierarchy.

Do you really need to dig:
MS uses some name conventions:
LPDWORD -L(ong)P(ointer) D(ouble)WORD
PDWORD - P(ointer)D(oubl e)WORD

But a typedef is just an alias not very much like inheritance.

Jun 27 '08 #4
"Carmen Sei" <fa**********@y ahoo.comwrote in message
news:19******** *************** *********@4ax.c om...
:I found in C++ a type is typedef several times and alot of time, I
: need to dig further to see the original type.
:
: like LPDWORD - it's a strange type, but when i dig further, i see it's
: a (DWORD *)

What you are observing is an idiosyncrasy of Windows headers,
not typical or recommended C++ style.

Most C++ experts will prefer to avoid declaring typedefs for
something as simple as a pointer. But there are some platform-
specific historical reasons for the use of some typedefs on
Windows, so you will see them used there...

: typedef DWORD near *PDWORD;
: typedef DWORD far *LPDWORD;
NB: near & far are non-standard C++ extensions that were
useful in 16-bit x86 times.

: i feel that creating new type in C++ by using "typedef" is similar to
: inheritance in Java -
It is not a new type, but another name for the exact same thing.

: where alot of time I need to dig further to found the "PARENT" type of
: an object hierarchy.
Indeed. This is why this style is not recommended. A new name
only should be introduced when there is a valid reason to.

Typedefs are normally only used (1) when there is a suspicion that
the underlying type might vary (e.g. on different platforms/target),
(2) when it provides a useful form of encapsulation (e.g. a class
containing a typedef that will be used by its clients), or (3) to
provide a shortcut for a complex type (in which case it is often
kept local, declared within the function where it is used).
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com

Jun 27 '08 #5
Then are you meaning

the following will compile only on Microsoft compiler but not other
C++ compiler?

typedef DWORD near *PDWORD;
typedef DWORD far *LPDWORD;

It's compiler specific added syntax (near / far) ??
>NB: near & far are non-standard C++ extensions that were
useful in 16-bit x86 times.
Jun 27 '08 #6
Carmen Sei wrote:
Then are you meaning
How may times have you and your other aliases been asked not to top-post?

--
Ian Collins.
Jun 27 '08 #7
On Thu, 17 Apr 2008 17:13:14 +1200, Ian Collins <ia******@hotma il.com>
wrote:
>Carmen Sei wrote:
>Then are you meaning
How may times have you and your other aliases been asked not to top-post?
top-post means I should not write on top of a message?
Jun 27 '08 #8
On Apr 17, 7:10 am, Carmen Sei <fatwallet...@y ahoo.comwrote:
Then are you meaning
the following will compile only on Microsoft compiler but not other
C++ compiler?
typedef DWORD near *PDWORD;
typedef DWORD far *LPDWORD;
It's compiler specific added syntax (near / far) ??
It's definitely compiler specific added syntax, not standard,
and will not compile on any of the Unix compilers I use. As
Ivan said, it's a platform specific extension, and not part of
the standard language. It's an extension which addressed a very
real problem on 16 bit Intel processors, however, which was
present (in one form or another) in all compilers for that
platform, and could very well be present in their descendents on
Windows today (for reasons of backward compatibility). It
wouldn't surprise me to see it in the Borland compiler as well,
for example.

I'd also like to expond on Ivan's point about not abusing
typedef's. A typedef results in information hiding. A good
thing, if the information is irrelevant to the user; a bad thing
otherwise. Something like:
typedef int WidgitCount ;
, for example, is good: all I need to know about WidgitCount is
that it is a counter (which the name tells me)---perhaps on
another platform, or in the future, it will be long, or even
BigInt. Typedef's like the above, however, hide the fact that
you're dealing with a pointer. The fact that you're dealing
with a pointer, however, is important information---you don't
use a pointer like you'd use the value itself. (If I'm not
mistaken, DWORD is also a typedef. And also equally abusive.)

Most of the time, you'll probably find you want a class, rather
than a typedef. A class introduces a totally new type; it also
allows you to add validations to the operations. (Of course,
for simple things like WidgitCount, a class might be overkill.
Theoretically nicer, but a lot of extra code for a very small
benefit.)

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

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

Similar topics

6
4261
by: Rex_chaos | last post by:
Hi all, I am going to do something just like the following tell template <class A> class Base { typedef Base& ref; public: void show(void)
6
1284
by: Keith H Duggar | last post by:
Often I've felt that the ability to alias an existing type where the alias would be treated as a different type thus allowing overloading, etc would be very useful. typedef : alias an existing type Foo as Bar where Bar is a Foo typecpy : alias an existing type Foo as Bar where Bar is not a Foo Here is a simple example : typecpy std::valarray<double> RowVector ;
4
2890
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that there shouldn't have been a "virtual" keyword for this purpose, but instead, a "nonvirtual" keyword! In teaching inheritance, you see the common example: class Vehicle {}
2
2687
by: Søren Holstebroe | last post by:
Hi there, I'm having a struggle with GNU g++ (3.3.5) and inheritance of typedefs in STL containers. I'm trying to port some old code I wrote with MS Visual C++ and it looks like there is a discrepancy in the STL implementation or at least interpretation of the standard (I trust g++ most on the latter). To the case: While this compiles fine:
3
2954
by: Generic Usenet Account | last post by:
This is a two-part question. (1) I have implemented a "Datastructure Registry" template class. I am getting no compiler warnings with older compilers, but newer compilers are generating the following error messages: warning: implicit typename is deprecated, please see the documentation for details Can someone kindly suggest how to get rid of these warnings? The source code follows.
1
2339
by: jsanga | last post by:
What are the major benifits / drawbacks to the following code? myheader.h #include <vector> struct MyStruct { int f1; float f2; ... };
26
2945
by: Mr.Tickle | last post by:
I want to give an alias for a type in C# so i can say SOMETYPEID refers to int etc, how can I do this on C#? something like typedef long SOMETYPEID in c++
22
23347
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
4
1952
by: subramanian100in | last post by:
Suppose the following program is named x.cpp #include <iostream> #include <vector> using namespace std; template <class T> class Vec : public vector<T> {
0
8395
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8310
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8826
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
6166
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4155
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.