Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Need clarification on 'typedef' keyword

Reply
Thread Tools

Need clarification on 'typedef' keyword

 
 
Krishna
Guest
Posts: n/a
 
      04-01-2005
I have three typedefs are defined below.

typedef char* PCHAR;
typedef const PCHAR PCCHAR1;
typedef const char* PCCHAR2;

In a piece of code, these typedef's are used as follows.

PCCHAR1 pc1 = text;
pc1 = text + 1;

PCCHAR2 pc2 = text;
pc2 = text + 1;

Now assigning a value to pc1 gives a compile error, while assigning a value
to pc2 doesn't give a compile error. The difference between the declaration
of PCCHAR1 and PCCHAR2 is that i have substituted "PCHAR" with "char*". I
had all along guessed that a typedef can be understood by just substituting.
Can someone please clear up my confusion?

Thanks & Regards,
Krishna


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-01-2005
Krishna wrote:
> I have three typedefs are defined below.
>
> typedef char* PCHAR;


'PCHAR' is a synonym for a pointer to char.

> typedef const PCHAR PCCHAR1;


'PCCHAR1' is a synonym for a constant pointer to char.

> typedef const char* PCCHAR2;


'PCCHAR2' is a synonym for a pointer to a constant char.

>
> In a piece of code, these typedef's are used as follows.
>
> PCCHAR1 pc1 = text;
> pc1 = text + 1;
>
> PCCHAR2 pc2 = text;
> pc2 = text + 1;
>
> Now assigning a value to pc1 gives a compile error, while assigning a
> value to pc2 doesn't give a compile error. The difference between the
> declaration of PCCHAR1 and PCCHAR2 is that i have substituted "PCHAR"
> with "char*". I had all along guessed that a typedef can be understood
> by just substituting. Can someone please clear up my confusion?


No, it cannot be understood by substituting like you did, it's not
a macro. It is essential to see it as a synonym for a type-id especially
WRT 'const' qualifier. 'const' relates to the object being defined. When
you declare a typedef, the new name is like an object. So,

typedef const BLAH CBLAH;

is the same as

typedef BLAH const CBLAH;

Now substitute whatever BLAH means here, and what part does the 'const'
affect? So, substitution analogy is only applicable if you place your
cv-qualifiers in the proper place, *after* the type name.

You do understand the difference between

const char * something;

and

char * const something;

, don't you?

V
 
Reply With Quote
 
 
 
 
BigBrian
Guest
Posts: n/a
 
      04-01-2005
The typedef for PCHAR1 is basically saying that PCHAR1 is a PCHAR that
is const. The typedef for PCHAR2 is basically saying that PCHAR2 is
a pointer to char that is const. Eventhough PCHAR is typdefed as a
char *, the typedefs for PCHAR1 and PCHAR2 are not the same. PCHAR1 is
a const PCHAR, and thus when you assign "text + 1" to it, the compiler
gives an error. However, pc2 is a pointer to char* that's const, and
thus "text + 1" can be assigned to it.

if you replace the first typedef with #define PCHAR char*, it compiles.
Thus, your believe that typedefs are the same as textual substitution
is not valid.

-Brian

 
Reply With Quote
 
Krishna
Guest
Posts: n/a
 
      04-01-2005

Victor wrote:

> Krishna wrote:
>
>> I have three typedefs are defined below.
>> typedef char* PCHAR;
>> typedef const PCHAR PCCHAR1;
>> typedef const char* PCCHAR2;
>> The difference between the
>> declaration of PCCHAR1 and PCCHAR2 is that i have substituted "PCHAR"
>> with "char*". I had all along guessed that a typedef can be
>> understood by just substituting. Can someone please clear up my
>> confusion?


> No, it cannot be understood by substituting like you did, it's not
> a macro. It is essential to see it as a synonym for a type-id
> especially WRT 'const' qualifier. 'const' relates to the object being

defined.
> When you declare a typedef, the new name is like an object.
>So,
> typedef const BLAH CBLAH;
>
> is the same as
>
> typedef BLAH const CBLAH;
>
> Now substitute whatever BLAH means here, and what part does the
> 'const' affect? So, substitution analogy is only applicable if you
> place your cv-qualifiers in the proper place, *after* the type name.


That clears the confusion!
So even though
typedef const BLAH CBLAH;
is syntactically correct, i guess its logically correct to always place the
const qualifier after the type name. That should be a better programming
practice?


> You do understand the difference between
> const char * something;
> and
> char * const something;
> , don't you?

I sure do.


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-01-2005
Krishna wrote:
> [...] So even though typedef const BLAH CBLAH;
> is syntactically correct, i guess its logically correct to always place
> the const qualifier after the type name. That should be a better
> programming practice?


Well... I'm often on the fence regarding "better programming practices".

For example, which is better
char *pchar;
or
char* pchar;
?

Here is another one: which is better
for (int i = 0; i < somenumber; i++)
or
for (int i = 0; i < somenumber; ++i)

The point is that for the beginner they might look different, while they
are really not, OTOH, one can argue that they do promote some good habits
in a novice. *I* use them interchangeably. Read: it doesn't matter where
you place 'const' as long as you know what you're getting, and why, and as
long as others (presumably knowledgeable C++ programmers as well) can see
and understand what you meant/intended.

Take this at its face value (since I am not paid to post here the value
of my posts can be seen as 0.0000) and form your own opinion. Philosophy
teaches us that in our heads we cannot have ideas that are not our own.
That means that whatever you end up doing or thinking is produced by your
own consciousness, your own mind. And you form those ideas based on the
information available to you, and nobody can tell you what to think or do.
Right?

V
 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      04-01-2005

"Victor Bazarov" <(E-Mail Removed)> wrote in message newslf3e.59016

> Philosophy
> teaches us that in our heads we cannot have ideas that are not our own.
> That means that whatever you end up doing or thinking is produced by your
> own consciousness, your own mind. And you form those ideas based on the
> information available to you, and nobody can tell you what to think or do.
> Right?
>


The voices in my head are screaming at me to disagree with you.



Happy April Fool's Day!

-Howard



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: keyword checker - keyword.kwlist Hamilton, William Python 4 05-13-2007 06:31 AM
keyword checker - keyword.kwlist tom@finland.com Python 6 05-10-2007 04:53 PM
need clarification with import statements Tool69 Python 2 12-15-2006 11:56 AM
Need clarification on asp_wp.exe,aspnet_wp.exe and aspnet_isapi.dll prem ASP .Net 1 11-03-2004 05:32 AM
Need some clarification and/or help with java NIO sockets HDSkiFreak Java 2 05-11-2004 01:48 PM



Advertisments