Click here to Skip to main content
16,007,126 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Setting Fonts in RTF Format Pin
mickelthepickle16-Dec-02 8:18
mickelthepickle16-Dec-02 8:18 
GeneralRe: Setting Fonts in RTF Format Pin
Shog916-Dec-02 10:37
sitebuilderShog916-Dec-02 10:37 
GeneralPassing Data from C++ Dll to VB-Application Pin
Anonymous16-Dec-02 3:46
Anonymous16-Dec-02 3:46 
GeneralRe: Passing Data from C++ Dll to VB-Application Pin
Ryan B.16-Dec-02 4:43
Ryan B.16-Dec-02 4:43 
Generalenum Pin
Rob Caldecott16-Dec-02 3:47
Rob Caldecott16-Dec-02 3:47 
GeneralRe: enum Pin
Maximilien16-Dec-02 4:14
Maximilien16-Dec-02 4:14 
GeneralRe: enum Pin
Tim Smith16-Dec-02 4:59
Tim Smith16-Dec-02 4:59 
GeneralRe: enum Pin
nde_plume16-Dec-02 6:06
nde_plume16-Dec-02 6:06 
The difference is to do with C/C++ differences.

In C, enums have a separate namespace of their own (as do
structs.) So when you say:

enum EColour {...}

You are introducing the name EColour into the enum namespace
in C. That means that subsequently to declare a type of this
kind you must qualify the name with enum, viz:

enum EColour myColour;

If you try to declare a variable thus in C:

EColour myColour;

You should get an error, because EColour is not a type name
in the global namespace it is in the enum namespace which is
separate. (Note some compilers blur the differences between
C and C++ and might accept the above.)

The second version:

typedef enum EColour { ... }

is a weird meaingless structure, that is allowed for various
reasons, however, the compiler basically ignores the typedef
keyword here. (typedef int; is also allowed, but is also
largely meaningless.) This is equivelent to the previous.

The third is a kind of old C hack.

typedef enum EColour {... } EColour;

In this case you are introducing two type names. In the enum
namespace you are introducing a type called EColour, and in
the global namespace another name, EColour that is typedefed
to enum EColour. (This statement is a combination of two
statements and is equivelent to:

enum EColour {...};
typedef enum EColour EColour;

In this case you can declare a variable of this type in two
ways:

enum EColour myColour;
EColour myColour;

Both are essentially equivelent, though they have different
types, the first is type "enum EColour" the second "EColour."
However, they are assignment compatible because C uses structural
type equivelence rather than name type equivelence (as in Pascal.)
That is to say, both enums are resolved to int in C.

Note that this is not idiomatic C. Normally this would be
written as typedef enum {red, green, blue } EColour; using
an anonymous enum in the enum space. However the structure
referred to is common with structs, viz:

typedef struct _List
{
struct _List* next;
int data;
} List;

This example above is largely the reason for this weirdness
in C. If we didn't have a name for the struct, viz:

typedef struct
{
List* next;
int data;
} List;

Is what we would like to write, however, when the compiler
finds the declaration of next it has not yet seen the declaration
of List, and so this is an error. Consequently, C added the
little hack to allow you to define the structure tag up front
as in the previous example, and then introduce the full name
at the end with type typedef. (Note in the first example you
must say struct _List not just _List. Note also, that it was
common practice to use a slighly modified name of the struct
like with an affixed "_" for the structure tag. The reason
is to be compatible with even more limited compilers from
the seventies.)

Things are slightly different in C++.

C++ places enum (and struct) names into the global namespace.
There is no special namespace for enums or structs.

When you do:

enum EColour {...};

In C++ that introduces a type name "EColour" into the global
namespace, as opposed to introducing "EColour" into the enum
namespace (As in C.)

Consequently,

typedef enum EColour {...} EColour;

is a duplication in C++ since you are introducing the name
twice. However, introducing a typename twice, assuming you
give it exactly the same definition is OK in C++. (Part of
the reason why this is so, is to allow for the aforementioned
construct, making C backward compatibility stronger.)

Finally, I said that C++ introduced the name into the global
namespace, that is not strictly true. In fact it introduces
it into the prevailing namespace at the point in code it is
declared. For example:

class VisualDescription
{
public:
enum EColour { red, green blue };
}

Introduces EColour into the VisualDescription namespace, since
it was declared in that class. That is why the correct way
to qualify these names is like this:

VisualDescription::EColour col = VisualDescription::red;

rather than:

EColour col = EColour::red;

In summary, the correct way to do this in C++ is

enum EColour { red, green. blue };

The other two examples you give a hacks left over from the
flaws in C's type system.

Oh, and one final thing, the List struct above is correctly
written thus in C++:

struct List
{
List* next;
int data;
};

Here the name List is introduced immediately in the first
line, and so is directly available when we declare "next".

Of course when List is introduced in the first line it
is introduced as an incomplete type, because the compiler
has not yet read the whole definition. However, pointers
to incomplete types are valid. (And incomplete types is
a whole other article.)

HTH

GeneralWM_HSCROLL message trapping Pin
ns16-Dec-02 3:40
ns16-Dec-02 3:40 
Generalpropertysheet focus agony Pin
Arthur Westerman16-Dec-02 3:00
Arthur Westerman16-Dec-02 3:00 
Questioncan body tell the simple code for executing a process in vc++? Pin
imran_rafique16-Dec-02 1:34
imran_rafique16-Dec-02 1:34 
AnswerRe: can body tell the simple code for executing a process in vc++? Pin
User 665816-Dec-02 2:10
User 665816-Dec-02 2:10 
GeneralRemoving registered tooltips Pin
Majid Shahabfar16-Dec-02 1:30
Majid Shahabfar16-Dec-02 1:30 
GeneralFlash player ocx Pin
eozkanal16-Dec-02 1:08
eozkanal16-Dec-02 1:08 
Generalmessage handling doesn't work Pin
Niko Tanghe16-Dec-02 1:05
Niko Tanghe16-Dec-02 1:05 
GeneralC runtime processing Pin
kingsley16-Dec-02 0:47
kingsley16-Dec-02 0:47 
GeneralRe: C runtime processing Pin
Christian Graus16-Dec-02 0:51
protectorChristian Graus16-Dec-02 0:51 
GeneralRe: C runtime processing Pin
Jeff J16-Dec-02 0:58
Jeff J16-Dec-02 0:58 
GeneralHELP!? Pin
User 665816-Dec-02 2:21
User 665816-Dec-02 2:21 
Generalillegal operation Pin
ArunKGoyal15-Dec-02 22:37
ArunKGoyal15-Dec-02 22:37 
GeneralRe: illegal operation Pin
Christian Graus15-Dec-02 22:53
protectorChristian Graus15-Dec-02 22:53 
GeneralRe: illegal operation Pin
ArunKGoyal15-Dec-02 23:22
ArunKGoyal15-Dec-02 23:22 
GeneralRe: illegal operation Pin
Daniel Strigl15-Dec-02 23:59
Daniel Strigl15-Dec-02 23:59 
GeneralRe: illegal operation Pin
ArunKGoyal16-Dec-02 0:30
ArunKGoyal16-Dec-02 0:30 
GeneralRe: illegal operation Pin
Daniel Strigl16-Dec-02 1:09
Daniel Strigl16-Dec-02 1:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.