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

C / C++ / MFC

 
GeneralRe: How to invoke a function at a regular interval? Pin
George223-Sep-03 16:44
George223-Sep-03 16:44 
GeneralRe: How to invoke a function at a regular interval? Pin
George223-Sep-03 16:45
George223-Sep-03 16:45 
GeneralQuestion about enum in C/C++ Pin
George223-Sep-03 1:48
George223-Sep-03 1:48 
GeneralRe: Question about enum in C/C++ Pin
567890123423-Sep-03 3:22
567890123423-Sep-03 3:22 
GeneralRe: Question about enum in C/C++ Pin
George223-Sep-03 16:51
George223-Sep-03 16:51 
GeneralRe: Question about enum in C/C++ Pin
Mike Dimmick23-Sep-03 4:03
Mike Dimmick23-Sep-03 4:03 
GeneralRe: Question about enum in C/C++ Pin
George223-Sep-03 16:49
George223-Sep-03 16:49 
GeneralRe: Question about enum in C/C++ Pin
Mike Dimmick24-Sep-03 9:11
Mike Dimmick24-Sep-03 9:11 
OK, let me try to explain namespaces again, a bit clearer this time hopefully!

Let's start with the global scope. In this scope, you can declare variables, functions and (through the typedef keyword) types.

In C, names that are introduced with the struct, union or enum keywords share another, separate, namespace which I'll call the structure namespace. Names can exist in both the global and the structure namespace with no problems. To access a name in the structure namespace, you must use the same keyword you used to declare it (i.e. if foo is an enumeration, you must use enum foo).

typedef can always be used to add a type name to the global namespace, whatever namespace the original type came from.

C++ changes the rules a bit. Types declared with the struct, union, enum and class keywords are still in a structure namespace; however, you no longer need to introduce them with the keyword when they're used.

Let's reproduce the example so it compiles:
 1 typedef enum foo_
 2 {
 3    FOO_BAR_1,
 4    FOO_BAR_2
 5 } foo;
 6  
 7 enum goo_
 8 {
 9    GOO_BAR_1,
10    GOO_BAR_2
11 };
12 
13 struct foo { int i; };
14 
15 int foo_;
The declaration at line 13 is NOT an error in C because the earlier foo at line 5 is a typedef name and hence is in the global namespace, while the declaration at 13 creates a foo in the structure namespace. It IS an error in C++ because it tries to introduce a new type with the same name.

However, I was wrong about line 15. It's OK in both C and C++. While you can't have a variable with the same name as a type in the global namespace, you're permitted to have one with the same name as a type in the structure namespace.

Results were compared with VC6 and with Comeau Computing's online compiler[^].

Note that I had to change the names of your enumerators (the values of the foo_ and goo_ enumerations). These are also added to the global namespace, and therefore would clash with each other if they had the same names.

Now to scopes: there is a 'global' and a 'structure' namespace within each scope that names can be declared in. A namespace using the C++ keyword is a named scope. Therefore you can refer to, for example, 'std::struct pair'.
GeneralRe: Question about enum in C/C++ Pin
George224-Sep-03 22:05
George224-Sep-03 22:05 
GeneralVectors Pin
bhangie23-Sep-03 0:56
bhangie23-Sep-03 0:56 
GeneralRe: Vectors Pin
YaronNir23-Sep-03 0:58
YaronNir23-Sep-03 0:58 
GeneralRe: Vectors Pin
jhwurmbach23-Sep-03 1:26
jhwurmbach23-Sep-03 1:26 
GeneralRe: Vectors Pin
bhangie23-Sep-03 1:31
bhangie23-Sep-03 1:31 
GeneralRe: Vectors Pin
bhangie23-Sep-03 1:36
bhangie23-Sep-03 1:36 
GeneralRe: Vectors Pin
jhwurmbach23-Sep-03 1:48
jhwurmbach23-Sep-03 1:48 
GeneralIcon sizing problem Pin
YaronNir23-Sep-03 0:56
YaronNir23-Sep-03 0:56 
GeneralRe: Icon sizing problem Pin
RobJones23-Sep-03 6:05
RobJones23-Sep-03 6:05 
GeneralRe: Icon sizing problem Pin
YaronNir23-Sep-03 6:11
YaronNir23-Sep-03 6:11 
GeneralRe: Icon sizing problem Pin
RobJones23-Sep-03 7:10
RobJones23-Sep-03 7:10 
GeneralChanging the DC's offset Pin
Zizilamoroso23-Sep-03 0:28
Zizilamoroso23-Sep-03 0:28 
GeneralRe: Changing the DC's offset Pin
vcplusplus23-Sep-03 3:14
vcplusplus23-Sep-03 3:14 
GeneralRe: Changing the DC's offset Pin
Zizilamoroso23-Sep-03 3:29
Zizilamoroso23-Sep-03 3:29 
GeneralRe: Changing the DC's offset Pin
vcplusplus23-Sep-03 7:21
vcplusplus23-Sep-03 7:21 
GeneralRe: Changing the DC's offset Pin
Zizilamoroso24-Sep-03 0:08
Zizilamoroso24-Sep-03 0:08 
GeneralRe: Changing the DC's offset Pin
vcplusplus24-Sep-03 1:56
vcplusplus24-Sep-03 1:56 

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.