Skip to main content
GameDev.net gamedev.net
🔒 Locked

anonymous union

Started by TEUTON Apr 20, 2006 at 1:01 AM 9 replies 800+ views
Original Post
TEUTON
TEUTON
I was reading about anonymous unions. Book said, if the anonymous union is global then it has to be static. Why is that?? and Why would I need to use anonymous union anyway??.
LessBread
LessBread
This might explain it: State of the union.


"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
TEUTON
TEUTON
Question still remains as it is
Article says:
You can declare an anonymous union in a file scope or a namespace scope. In these cases, however, it must be declared static and its members have internal linkage.

What if I don't what to have a filescope??
Sneftel
Sneftel
Then don't make it anonymous.
TEUTON
TEUTON
I get your point but why is anonymous restricted to file scope. That is what I am asking.
Sneftel
Sneftel
Consider an anonymous union with external linkage. How would you access its members from a different file scope?
Illco
Illco
Quote:

I get your point but why is anonymous restricted to file scope. That is what I am asking.

That's not what it says. It says if you are defining a union at file scope, then it has to be declared static. You can use anonymous unions in for example a class and in this case it doesn't necessarily have to be static.

Quote:

What if I don't what to have a filescope??

Then it will become pretty hard to compile anything I suppose -- it's the only scope you have by default. And, referring to the first point, then you can put your anonymous union in a namespace for example.

Quote:

If the anonymous union is global then it has to be static. Why is that?

I suppose this is your main question. I don't know the answer, and I may be off here but isn't anything in global scope inherently static?

Illco
TEUTON
TEUTON
Quote:
Original post by Illco
but isn't anything in global scope inherently static?


I don't think so.

LessBread
LessBread
Quote:
Original post by Illco
... but isn't anything in global scope inherently static?


No. "When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared)." static

"Declarations of variables and functions at file scope are external by default." extern

"The name of each member must be unique within the scope where the union is declared." Anonymous Union

The limiting factor here seems to me to be the linker.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
TEUTON
TEUTON
Quote:
Original post by LessBread
"The name of each member must be unique within the scope where the union is declared."


But isn't that also applicable to the same file.

I guess restriction is to avoid such name clashes across multifile programs.

LessBread
LessBread
Quote:
Original post by TEUTON
Quote:
Original post by LessBread
"The name of each member must be unique within the scope where the union is declared."


But isn't that also applicable to the same file.


That would be file scope ???

Quote:
Original post by TEUTON
I guess restriction is to avoid such name clashes across multifile programs.


Yes.

"Linkage: All external references are resolved to create an executable program or a dynamic-link library." Phases of Translation

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.