PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Union and class

Hi all.

I've hit a snag that I can't seem to get around.

I'm converting some floating point code to use my fixed point class.

It all works pretty well, except at one place where there is a union where float is used as a member and I cannot use my fixed point class since it creates an error C2620:

member 'fValue' has user-defined constructor or non-trivial default constructor

What can I do to get around this? I've never used unions, and I'm not sure what to do.

My FixedPoint class has several constructors. One takes no paramater, other takes one (int, long, double, float, etc).

Here is the declaration:

typedef union
{
int iValue;
float fValue;
void *pValue;
} DValue;

I would like to replace the "float fValue" with "FixedPoint fValue" but I cannot due to explained cause.

Thanks for any help!
Put it in a nameless structure, it will work:


Code:

union
{
...
struct
{
FixedPoint fValue
};
...
};
Thierry wrote:
Put it in a nameless structure, it will work:

...until you have a vtable.
...until you have multiple classes in the union with constructors.
...until you use a compiler other than MSVC (or possibly future versions.)

If none of these conditions will ever apply, then you won't need to worry. :wink:

The Standard - sec. 9.5 wrote:A union shall not have base classes. A union shall not be used as a base class. An object of a class with a non-trivial default constructor, a non-trivial copy constructor, a non-trivial destructor, or a non-trivial copy assignment operator cannot be a member of a union, nor can an array of such objects. If a union contains a static data member, or a member of reference type, the program is ill-formed.
MeumSimulacrum (m) · 23 May 2003, 19:15 UTC
wow
Does your fixed point class has a method for retrieving internal representation of the value (dword or whatever ) ?

If it does then stick that internal value into union and then simply reinitialize your fixed point class with a value stored in the union.
Even if that works it seems a bit shady. Theirry what actually is that doing?


But on another note what is FixedPoint, integer class with methods or just a typedef?
Sm!rk wrote:
Even if that works it seems a bit shady. Theirry what actually is that doing?


But on another note what is FixedPoint, integer class with methods or just a typedef?


It basically does nothing. i.e. None of FixedPoint's constructors get called.

Guess what Mr. Standard-Ass-Licker: there is only one compiler available for the Pocket PC, and what I said works on it. So where is the problem?


There wasn't a problem, he stated that he was unfamiliar with unions and I was simply trying to provide some information that might help him from running into a similar roadblock in the future.


I most probably know the C++ standard more then you ever will. But I guess your comments perfectly describe the difference between a software engineer who gets the work done vs Mr. Know-it-all. Go back to alt.comp.lang.c++ where you belong.


You seem to know a lot about me, have we met?
What? A programmer fight? Wow, this is a first for me.
battle of the super geeks...i love it...

imagine, a fight over the proper syntax of a union in c++...it almost makes me cry...

Thierry...i dig ya man...but going against the standards is what they call a 'kludge' or a 'hack' :) Now, this is not to say i haven't done it myself...but one should be hesitant to do such things as they tend to have very odd, very unpredictable behavior...

I mean, we've all seen it...some completely random line blows up because you did something bad in a totally different place.
Thierry wrote:Put it in a nameless structure, it will work:

Thierry, thanks a lot it worked nicely.


It basically does nothing. i.e. None of FixedPoint's constructors get called.

That's actually a good thing to know, standard or not.


It basically does nothing. i.e. None of FixedPoint's constructors get called.

If you want to see a programmer's holy war, go to some C++ newsgroup as ask which is a better syntax:
int *v or int* v, and enjoy the stupidest posts you can find! ;)

--Eric
int* v, naturally... it's logical to keep the variable name and type separate.
Of course, but then again... int *v looks much better :-)
yeah, well, what's wrong with int*v or int * v? Both look awesome.
I wasn't even arguing syntax or saying the anonymous struct was a bad thing either, which is what makes this fun so far.

All I'm trying to say is be careful about what you stick into a union even though the compiler might let you.
Code:

class NonTrivialConstructor {
public:
NonTrivialConstructor() { i = 696969; }
int i;
};

class NonTrivialConstructor2 {
public:
NonTrivialConstructor2() { i = 4096; }
int i;
};

union Combo {
int v1;
struct {
NonTrivialConstructor v2;
};
struct {
NonTrivialConstructor2 v3;
};
};


So which constructor is used?

With the stars in this particular alignment (vs.net 2003,) v3's constructor is the one that ends up taking precedance. I don't have any guarantees that this will always be the case either.

One way to avoid this entirely would be to the union's constructor instead.
I for one am always happy to be informed more about the standard (since I REALLY hate reading it). How this spawned into a flame war is beyond me.
It should be:
int *v;

And here's why. The pointer operator is not part of the type. For example:

int* a, b, c;

One would take that to mean:
int *a;
int *b;
int *c;

When it really is:
int *a;
int b;
int c;

Now this:
int *a, b, c;

Grammatically indicates what is actually occurring in the definition, so there is no confusion.

Dan East
Dan East wrote:
It should be: int *v;

And here's why. The pointer operator is not part of the type. For example:

int* a, b, c;

Hello Dan.

When I started in C++ I remember I used to write int* a,b,c; I was under the impression that int* was a pointer type, and all the variable names that followed were pointers.

Writing int *a,*b,*c; makes it much clearer.

Since I've been there and done that mistake, I can safely say that this is good information for anyone starting in C++.

--Eric
I could be wrong but that was an issue with C, which is why the major stylistic use was [int *p], since [int *p, *q, *r] was the only way to do it (short of seperate lines).

However, in C++ this was "fixed", now its [int* p], since [int* p, q, r] makes 3 int pointers.

Personally I use [int * p], I only use it next to a variable when I'm using it as a operation.
Code:

int* a, b, c;
a=b;


//error C2440: '=' : cannot convert from 'int' to 'int *'
//Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast


That is compiling a cpp file in VC++ 6.0.

It's very simple. Array and pointer modifiers are specific to the variable, not the type. For example, you can't do this:

int[64] a, b, c;

Dan East
But does the initialization method automatically make int* a bad stylistic choice?
since [int* p, q, r] makes 3 int pointers.

This is not correct.

Enter that in VS, and when your run your app move the mouse over p. It will say [int *p], over q it will say [int q].

--Eric
Dan East wrote:
Code:

int* a, b, c;
a=b;



True, true. But at the same time you'd get a lot of arguments against declaring multiple variables one line as well. All those nice idioms like "avoid gratuitous default constructors", "construct at first use" don't work at all when you do that :lol:

Placement of the * can have a communicative aspect to it as well - placing it by a type means you're declaring, placing it by the variable means you're using indirection. At least, that's how I justify it to myself.

In the end, it doesn't really matter. When you code for yourself, you do it how ever the hell you feel like. When you code for someone else, you follow their style, no matter how stupid it may be. In the end, it's better to have a consistent style across a project than to have little scattered fragments of everyone's stylistic ego :wink:
Velo wrote:"construct at first use"


Okay, now we're definitely not just talking about C. :)

Dan East
I told you this was going to degenerate ;)

--Eric
And it's all thanks to me :twisted: