?
Solved

static anonymous union member

Posted on 2003-03-20
10
Medium Priority
?
305 Views
Last Modified: 2007-12-19
static member variables have to be declared e.g.
class A{
    static int i;
}

int A::i = 0;

However, what if the variable is an anonymous union e.g.
class B{
    static union{
        int*  pint;
        char* pchar;
    }
}

how do I declare this?

This doesn't work
int*  B::pint;
char* B::pchar;

Nor does this
static union{
    int*  B::pint;
    char* B::pchar;
}
0
Comment
Question by:dja98
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 8172970
Class B, as you wrote it, doesn't contain static member.  But if we add this member, there is a way to declare it outside of class. This code fragment is compiles in VC++ :

class B
{
public:
   static union
   {
       int*  pint;
       char* pchar;
   } u;
};

static union B::__unnamed u;

int main(int argc, char* argv[])
{
    u.pchar = 0;

     return 0;
}


0
 

Author Comment

by:dja98
ID: 8173010
But I want to be able to access it as (for example)

int main(blahblah)
{
    B::pchar = "Hello";
    printf("%i\n", B::pint);
}
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 8173039
class B
{
   static union
   {
       int*  pint;
       char* pchar;
   };
}

There is nothing to access here, because union is data type and not object. You can access it's members only if you declare variable of such type.

Suppose you declare structure:

struct sA
{
    int n;
    chas c;
};

The only way to work with sA::n or sA::c is to declare variacle of type sA:

sA a;
a.n = 0;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dja98
ID: 8173067
Surely that is only for 'typedef union{blahblah}'.  Otherwise what is the point of an anonymous union of any type?
0
 
LVL 4

Accepted Solution

by:
n_fortynine earned 750 total points
ID: 8173076
GCC compiler gives the error:
line 6 # An anonymous union that is a member of a class must not be static.
as far as i know this acts like a global anonymous union. Why have it as a member of the class if you intend to make it global?

0
 

Author Comment

by:dja98
ID: 8173107
In my code it would have been private - if I can't do it I'll have to rethink - thanks
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 8173153
OK, I got your idea. Trying to play with it I found that VC++ complier ignores static keyword for anonymous union and treats it always as non-static member:

class B
{
public:
   static union
   {
       int*  pint;
       char* pchar;
   };
};


int main(int argc, char* argv[])
{
    B b;
    b.pint = 0;

    B b1;
    b1.pint = new int(2);
    delete[] b1.pint;


    return 0;
}

It is interesting what happens with other C++ compilers.
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8173509
Microsoft go out of therr way to do so many things standard C++ wouldn't allow that I almost hate to use their compiler. =)
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 8173587
They promise 100% ANSI C++ compatibility in Visual Studio 2003.
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 8174669
> They promise 100% ANSI C++ compatibility in Visual Studio 2003.

I don't think they promise 100% compatibility (e.g. export is still not supported) - I don't think any compiler is 100% compatible yet.  However, it has gone from probably last place in the C++ standards compliance league, to probably first place.  
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question