static anonymous union member

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;
}
dja98Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
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
dja98Author Commented:
But I want to be able to access it as (for example)

int main(blahblah)
{
    B::pchar = "Hello";
    printf("%i\n", B::pint);
}
0
AlexFMCommented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

dja98Author Commented:
Surely that is only for 'typedef union{blahblah}'.  Otherwise what is the point of an anonymous union of any type?
0
n_fortynineCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dja98Author Commented:
In my code it would have been private - if I can't do it I'll have to rethink - thanks
0
AlexFMCommented:
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
n_fortynineCommented:
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
AlexFMCommented:
They promise 100% ANSI C++ compatibility in Visual Studio 2003.
0
jasonclarkeCommented:
> 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.