Jump to content
  • Advertisement
Sign in to follow this  
TEUTON

anonymous union

This topic is 4536 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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??.

Share this post


Link to post
Share on other sites
Advertisement
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??

Share this post


Link to post
Share on other sites
Consider an anonymous union with external linkage. How would you access its members from a different file scope?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Quote:
Original post by Illco
but isn't anything in global scope inherently static?


I don't think so.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!