|
|
|
|
|
|
| Author |
Message |
Tak-Shing Chan *nix forums beginner
Joined: 10 Jul 2006
Posts: 19
|
Posted: Fri Jul 21, 2006 1:50 pm Post subject:
Re: Tagged unions
|
|
|
On Fri, 21 Jul 2006, Johan Tibell wrote:
| Quote: | I use a tagged union to represent different expression types in one of
my programs.
struct exp {
enum {
LIT,
VAR
} type;
union {
int lit;
char *var;
} form;
};
In my implementation I've put the enum outside of the struct and given
it a name, "exp_type".
enum exp_type { /* ... */ };
struct my_struct {
enum exp_type type;
/* ... */
};
What would be the pros and cons of having it unnamed inside the struct
versus named outside the struct respectively? I can think of a few:
Pros:
* Less pollution of the namespace. I currently have two different
structs so I have to prefix my enum type names with "structname_" (e.g.
exp_type).
* Saves me some typing.
* Avoid repetition of the name "type" in the variable declaration
inside the struct (e.g. exp_type type).
Cons:
* Can't create a variable of the enum type since the type can't be
referred to. (Would it even be possible to refer to the enum type if it
was named _and_ declared inside the struct?). This can also be a good
thing if no more variables of the enum type will ever be created but it
can be a bit difficult to predict in advance.
|
Why would you ever need to create such variables? That is
bad programming practice in my book (creating unnecessary
couplings).
| Quote: | This is probably more of a stylistic question than anything else (and
hence I expect 10^100 replies).
|
My preference is to use anonymous enums in this situation.
Tak-Shing |
|
| Back to top |
|
 |
johan.tibell@gmail.com *nix forums beginner
Joined: 18 Jul 2006
Posts: 3
|
Posted: Fri Jul 21, 2006 1:00 pm Post subject:
Tagged unions
|
|
|
I use a tagged union to represent different expression types in one of
my programs.
struct exp {
enum {
LIT,
VAR
} type;
union {
int lit;
char *var;
} form;
};
In my implementation I've put the enum outside of the struct and given
it a name, "exp_type".
enum exp_type { /* ... */ };
struct my_struct {
enum exp_type type;
/* ... */
};
What would be the pros and cons of having it unnamed inside the struct
versus named outside the struct respectively? I can think of a few:
Pros:
* Less pollution of the namespace. I currently have two different
structs so I have to prefix my enum type names with "structname_" (e.g.
exp_type).
* Saves me some typing.
* Avoid repetition of the name "type" in the variable declaration
inside the struct (e.g. exp_type type).
Cons:
* Can't create a variable of the enum type since the type can't be
referred to. (Would it even be possible to refer to the enum type if it
was named _and_ declared inside the struct?). This can also be a good
thing if no more variables of the enum type will ever be created but it
can be a bit difficult to predict in advance.
This is probably more of a stylistic question than anything else (and
hence I expect 10^100 replies). |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Thu Nov 20, 2008 8:14 am | All times are GMT
|
|
Loans | Bad Credit Loan | Remortgaging | Remortgaging | Mortgage Calculator
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|