From: Igor Tandetnik on 15 Sep 2009 19:13 Robby <Robby(a)discussions.microsoft.com> wrote: > ===================== > struct tag_ts > { > int on_activation_state_1; > long ts_area_1; > long relevant_ctrl_msg_1; > }ts, *p_ts; > ===================== > > I don't get what it would change if we leave it like this.... If you put this into a header file, and #include it into more than one source file, you'll get linker errors. > All my other structures are typedefed Typedef is a red herring. You'll have the same problem if you put this into a header file: int ts, *p_ts; then include it into several sources. The important part is that you should only _declare_ variables in a header file, then _define_ them in exactly one source file. > but to refuse a special > one time declaration of one structure globally, do we really > sincerely believe that it is that critical. It's not the declaration of a structure that causes the problem - it's the definition of a variable. As to believing - this is not a matter of belief. Your program will fail to compile. Try convincing the compiler that its belief system is unreasonable. > We have to give a better > argument as to why this is so unacceptable... Compiler errors aren't a good enough argument? > I > know it breaks the golden rule of not defining strucutres globally You misstate the rule. This may be the source of your confusion. -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
From: Scot T Brennecke on 16 Sep 2009 00:22 Igor Tandetnik wrote: > David Webber <dave(a)musical-dot-demon-dot-co.uk> wrote: >> "Robby" <Robby(a)discussions.microsoft.com> wrote in message >> news:1BE3A450-F943-45B0-A25D-7D13980D761B(a)microsoft.com... >> >>> But suppose I just need a structure to hold some data globally and I >>> know I >>> will never ever need more than one instance. It will simply reside >>> globally >>> and will be accessed whenever required by other functions.... >> In circumstances like that I tend to declare structures with all >> members "static". > > No such thing exists in C. > >> Then to access the data, you simply use >> >> MyStruct.element; > > Surely you mean MyStruct::element > >> It's a poor man's namespace really - always a good idea for global >> data! > > Why not just use an actual namespace? Because scope resolution and namespaces don't exist in C?
From: Igor Tandetnik on 16 Sep 2009 00:30 Scot T Brennecke wrote: > Igor Tandetnik wrote: >> David Webber <dave(a)musical-dot-demon-dot-co.uk> wrote: >>> "Robby" <Robby(a)discussions.microsoft.com> wrote in message >>> news:1BE3A450-F943-45B0-A25D-7D13980D761B(a)microsoft.com... >>> >>>> But suppose I just need a structure to hold some data globally and >>>> I know I >>>> will never ever need more than one instance. It will simply reside >>>> globally >>>> and will be accessed whenever required by other functions.... >>> In circumstances like that I tend to declare structures with all >>> members "static". >> >> No such thing exists in C. >> >>> Then to access the data, you simply use >>> >>> MyStruct.element; >> >> Surely you mean MyStruct::element >> >>> It's a poor man's namespace really - always a good idea for global >>> data! >> >> Why not just use an actual namespace? > > Because scope resolution and namespaces don't exist in C? Neither do static struct members, so David must have been talking about C++. -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
From: Scot T Brennecke on 16 Sep 2009 00:39 Igor Tandetnik wrote: > Scot T Brennecke wrote: >> Igor Tandetnik wrote: >>> David Webber <dave(a)musical-dot-demon-dot-co.uk> wrote: >>>> "Robby" <Robby(a)discussions.microsoft.com> wrote in message >>>> news:1BE3A450-F943-45B0-A25D-7D13980D761B(a)microsoft.com... >>>> >>>>> But suppose I just need a structure to hold some data globally and >>>>> I know I >>>>> will never ever need more than one instance. It will simply reside >>>>> globally >>>>> and will be accessed whenever required by other functions.... >>>> In circumstances like that I tend to declare structures with all >>>> members "static". >>> No such thing exists in C. >>> >>>> Then to access the data, you simply use >>>> >>>> MyStruct.element; >>> Surely you mean MyStruct::element >>> >>>> It's a poor man's namespace really - always a good idea for global >>>> data! >>> Why not just use an actual namespace? >> Because scope resolution and namespaces don't exist in C? > > Neither do static struct members, so David must have been talking about > C++. OK, so you were just carrying on a side conversation with David. I mistook those to be suggestions as better advice for Robby.
From: Ulrich Eckhardt on 16 Sep 2009 03:04 Robby wrote: > Okay, I know that defining structures globally is not a good programming > habit and boy did I learn that one from you guys. So we should always > typedef the structs so that we are able to reuse them whereever we need > them. This doesn't make sense. You typically define structs (or enumerations, unions and even typedefs) globally. What you don't do regularly is to define objects (i.e. instances of structs, unions, enumerations or builtin types). Using a typedef with a struct allows you to leave out the "struct" from an object declaration: struct foo { ... }; foo f; // wrong typedef struct whatever { ... } bar; bar b; // correct Uli -- C++ FAQ: http://parashift.com/c++-faq-lite Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: Adding a button to CStatusbar of a SDI. Next: pow function w/o math.h |