473,805 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

union float[3] and x,y,z

Uhm...
Is there any way of making
float pos[3];
a union of
float x,y,z;

somehow, so I can use:
mything[0] = mything.y;
instead of mything.x = mything.y

Thanks in advice,

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%c gl%ssic%ccom%c" , "ma", 58, 'g', 64, "ba", 46, 10);}

Sep 25 '07 #1
8 3715
On Sep 25, 10:26 pm, "Gernot Frisch" <M...@Privacy.n etwrote:
Uhm...
Is there any way of making
float pos[3];
a union of
float x,y,z;

somehow, so I can use:

mything[0] = mything.y;
instead of mything.x = mything.y

Thanks in advice,

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%c gl%ssic%ccom%c" , "ma", 58, 'g', 64, "ba", 46, 10);}
Hi Gernot,

Just a thought. How about making a structure and overriding operators?
Cheers,

Sep 25 '07 #2
Gernot Frisch wrote:
Uhm...
Is there any way of making
float pos[3];
a union of
float x,y,z;

somehow, so I can use:
mything[0] = mything.y;
instead of mything.x = mything.y

Thanks in advice,
Hasn't this been discussed here a couple of times over the past
two-three months? No, there is no sense in making a union. The
only way you can manage that is if you stuff 'x', 'y', and 'x' in
some kind of struct inside that union. In structs members can
have padding, and in arrays they don't. So, it is conceivable
that your union trick is not going to work. A solution might be

struct Blah {
float pos[3];
float &x, &y, &z;
Blah() : x(pos[0]), y(pos[1]), z(pos[2]) {}
};

which makes it non-POD (members that are references). Or agree
to define 'x' as a function:

struct Blah {
float pos[3];
float& x() { return pos[0]; }
float x() const { return pos[0]; }
};

In any case, search the archives for "union padding array" and
some other keywords you can come up with.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 25 '07 #3
On 2007-09-25 14:26, Gernot Frisch wrote:
Uhm...
Is there any way of making
float pos[3];
a union of
float x,y,z;

somehow, so I can use:
mything[0] = mything.y;
instead of mything.x = mything.y
No, however you can do something like this:

struct thing
{
float arr[3];
float& x;
float& y;
float& z;

thing() : x(arr[0]), y(arr[1]), z(arr[2]) {}
};

Which allows you to access x through either t.x or through t.arr[0]
where t is an object of type thing:

int main()
{
thing t;
t.x = 1;
t.y = 2;
t.arr[2] = 3;

int i = 0;
}

If you want to be able to use t[0] instead of t.arr[0] you have to
overload the [] operator as Alexander Dong Back Kim suggested.

--
Erik Wikström
Sep 25 '07 #4
Erik Wikström wrote:
On 2007-09-25 14:26, Gernot Frisch wrote:
>Uhm...
Is there any way of making
float pos[3];
a union of
float x,y,z;

somehow, so I can use:
mything[0] = mything.y;
instead of mything.x = mything.y

No, however you can do something like this:

struct thing
{
float arr[3];
float& x;
float& y;
float& z;

thing() : x(arr[0]), y(arr[1]), z(arr[2]) {}
};

Which allows you to access x through either t.x or through t.arr[0]
where t is an object of type thing:

int main()
{
thing t;
t.x = 1;
t.y = 2;
t.arr[2] = 3;

int i = 0;
}

If you want to be able to use t[0] instead of t.arr[0] you have to
overload the [] operator as Alexander Dong Back Kim suggested.
Actually, could still do that if you do this trick:

struct thing {
float x, y, z;
struct thing_indexing {
thing& t;
thing_indexing( thing& t) : t(t) {}
float& operator[](int i) {
return i < 2 ? i < 1 ? t.x : t.y : t.z;
}
} arr;
thing() : arr(*this) {}
thing& operator=(thing const& t) {
x = t.x;
y = t.y;
z = t.z;
}
};

int main() {
thing t;
t.x = 3.1415926;
t.arr[1] = t.x;
}

RATS! I got pulled in and am writing code when I didn't want to....

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 25 '07 #5
On 2007-09-25 15:26, Victor Bazarov wrote:
Erik Wikstr�m wrote:
>On 2007-09-25 14:26, Gernot Frisch wrote:
>>Uhm...
Is there any way of making
float pos[3];
a union of
float x,y,z;

somehow, so I can use:
mything[0] = mything.y;
instead of mything.x = mything.y

No, however you can do something like this:

struct thing
{
float arr[3];
float& x;
float& y;
float& z;

thing() : x(arr[0]), y(arr[1]), z(arr[2]) {}
};

Which allows you to access x through either t.x or through t.arr[0]
where t is an object of type thing:

int main()
{
thing t;
t.x = 1;
t.y = 2;
t.arr[2] = 3;

int i = 0;
}

If you want to be able to use t[0] instead of t.arr[0] you have to
overload the [] operator as Alexander Dong Back Kim suggested.

Actually, could still do that if you do this trick:

struct thing {
float x, y, z;
struct thing_indexing {
thing& t;
thing_indexing( thing& t) : t(t) {}
float& operator[](int i) {
return i < 2 ? i < 1 ? t.x : t.y : t.z;
}
} arr;
thing() : arr(*this) {}
thing& operator=(thing const& t) {
x = t.x;
y = t.y;
z = t.z;
}
};

int main() {
thing t;
t.x = 3.1415926;
t.arr[1] = t.x;
}

RATS! I got pulled in and am writing code when I didn't want to....
Sorry, but I just cannot understand the reason for this construct. To me
it looks like it provides exactly the same functionality as my code,
except that I used an array and you used a struct with an overloaded []
operator. Please, enlighten me.

--
Erik Wikström
Sep 25 '07 #6
Erik Wikström wrote:
On 2007-09-25 15:26, Victor Bazarov wrote:
>Erik Wikstr?m wrote:
>>On 2007-09-25 14:26, Gernot Frisch wrote:
Uhm...
Is there any way of making
float pos[3];
a union of
float x,y,z;

somehow, so I can use:
mything[0] = mything.y;
instead of mything.x = mything.y

No, however you can do something like this:

struct thing
{
float arr[3];
float& x;
float& y;
float& z;

thing() : x(arr[0]), y(arr[1]), z(arr[2]) {}
};

Which allows you to access x through either t.x or through t.arr[0]
where t is an object of type thing:

int main()
{
thing t;
t.x = 1;
t.y = 2;
t.arr[2] = 3;

int i = 0;
}

If you want to be able to use t[0] instead of t.arr[0] you have to
overload the [] operator as Alexander Dong Back Kim suggested.

Actually, could still do that if you do this trick:

struct thing {
float x, y, z;
struct thing_indexing {
thing& t;
thing_indexing( thing& t) : t(t) {}
float& operator[](int i) {
return i < 2 ? i < 1 ? t.x : t.y : t.z;
}
} arr;
thing() : arr(*this) {}
thing& operator=(thing const& t) {
x = t.x;
y = t.y;
z = t.z;
}
};

int main() {
thing t;
t.x = 3.1415926;
t.arr[1] = t.x;
}

RATS! I got pulled in and am writing code when I didn't want to....

Sorry, but I just cannot understand the reason for this construct. To
me it looks like it provides exactly the same functionality as my
code, except that I used an array and you used a struct with an
overloaded [] operator. Please, enlighten me.
It was a facetious suggestion, so that the OP could use 'arr' to get
to the members, not just the indexing operator (working from your
original suggestion to use 'thing.arr[0]' syntax).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 25 '07 #7
"Victor Bazarov" <v.********@com Acast.netwrote in message
news:fd******** **@news.datemas .de...
Erik Wikström wrote:
>On 2007-09-25 15:26, Victor Bazarov wrote:
>>Erik Wikstr?m wrote:
On 2007-09-25 14:26, Gernot Frisch wrote:
Uhm...
Is there any way of making
float pos[3];
a union of
float x,y,z;
>
somehow, so I can use:
>
>
mything[0] = mything.y;
instead of mything.x = mything.y

No, however you can do something like this:

struct thing
{
float arr[3];
float& x;
float& y;
float& z;

thing() : x(arr[0]), y(arr[1]), z(arr[2]) {}
};

Which allows you to access x through either t.x or through t.arr[0]
where t is an object of type thing:

int main()
{
thing t;
t.x = 1;
t.y = 2;
t.arr[2] = 3;

int i = 0;
}

If you want to be able to use t[0] instead of t.arr[0] you have to
overload the [] operator as Alexander Dong Back Kim suggested.

Actually, could still do that if you do this trick:

struct thing {
float x, y, z;
struct thing_indexing {
thing& t;
thing_indexing( thing& t) : t(t) {}
float& operator[](int i) {
return i < 2 ? i < 1 ? t.x : t.y : t.z;
}
} arr;
thing() : arr(*this) {}
thing& operator=(thing const& t) {
x = t.x;
y = t.y;
z = t.z;
}
};

int main() {
thing t;
t.x = 3.1415926;
t.arr[1] = t.x;
}

RATS! I got pulled in and am writing code when I didn't want to....

Sorry, but I just cannot understand the reason for this construct. To
me it looks like it provides exactly the same functionality as my
code, except that I used an array and you used a struct with an
overloaded [] operator. Please, enlighten me.

It was a facetious suggestion, so that the OP could use 'arr' to get
to the members, not just the indexing operator (working from your
original suggestion to use 'thing.arr[0]' syntax).
Isn't this functionality what the OP asked for, what Alexander was
suggesting?

struct thing
{
float x, y, z;
float& operator[]( size_t Index )
{
switch ( Index )
{
case 0:
return x;
case 1:
return y;
case 2:
return z;
default:
throw "Bad Index";
}
}
};

I'm confused where this "arr" come in, I don't see it in the OP.
Sep 26 '07 #8
Gernot Frisch wrote:
Uhm...
Is there any way of making
float pos[3];
a union of
float x,y,z;

somehow, so I can use:
mything[0] = mything.y;
instead of mything.x = mything.y
Why stop there? why not make point a container:

#include <cstddef>
#include <cassert>
#include <iterator>
#include <functional>
#include <algorithm>

template < typename T >
struct const_qualifier {

typedef T type;

typedef T mutable_type;
typedef T const const_type;

static bool const is_const = false;

};

template < typename T >
struct const_qualifier < T const {

typedef T const type;

typedef T mutable_type;
typedef T const const_type;

static bool const is_const = true;

};

template < typename T, bool is_const >
struct pointer_paramet er;

template < typename T >
struct pointer_paramet er<T,true{

typedef T const * type;

};

template < typename T >
struct pointer_paramet er<T,false{

typedef T * type;

};

template < typename T, bool is_const >
struct reference_param eter;

template < typename T >
struct reference_param eter<T,true{

typedef T const & type;

};

template < typename T >
struct reference_param eter<T,false{

typedef T & type;

};

template < typename Target, typename C, bool is_const >
struct indexing_iterat or_base;

template < typename Target, typename C >
struct indexing_iterat or_base<Target, C,false:
public std::iterator< std::random_acc ess_iterator_ta g,
typename C::difference_t ype,
typename C::pointer,
typename C::reference >
{
C * the_container;
typename C::size_type the_index;

indexing_iterat or_base ( C * c,
typename C::size_type i )
: the_container ( c )
, the_index ( i )
{}

typename C::reference operator* ( void ) {
return ( the_container->operator[]( the_index ) );
}

typename C::pointer operator-( void ) {
return ( & this->operator*() );
}

};

template < typename Target, typename C >
struct indexing_iterat or_base<Target, C,true:
public std::iterator< std::random_acc ess_iterator_ta g,
typename C::difference_t ype,
typename C::const_pointe r,
typename C::const_refere nce >
{

C const * the_container;
typename C::size_type the_index;

indexing_iterat or_base ( C const * c,
typename C::size_type i )
: the_container ( c )
, the_index ( i )
{}

typename C::const_refere nce operator* ( void ) const {
return ( the_container->operator[]( the_index ) );
}

typename C::const_pointe r operator-( void ) const {
return ( & this->operator*() );
}

};

template < typename IndexedCont,
bool is_const = const_qualifier <IndexedCont>:: is_const >
struct indexing_iterat or
: public indexing_iterat or_base< indexing_iterat or<IndexedCont, is_const>,
IndexedCont, is_const >
{

typedef IndexedCont container_type;
typedef typename container_type: :size_type size_type;

private:

typedef indexing_iterat or_base< indexing_iterat or<IndexedCont, is_const>,
IndexedCont, is_const my_base;

using typename my_base::refere nce;
using typename my_base::pointe r;

public:

indexing_iterat or ( typename reference_param eter< container_type, is_const
>::type the_cont,
size_type the_index )
: my_base ( &the_cont, the_index )
{}

indexing_iterat or & operator++ ( void ) {
++ my_base::the_in dex;
return ( *this );
}

indexing_iterat or & operator-- ( void ) {
-- my_base::the_in dex;
return ( *this );
}

indexing_iterat or operator++ ( int ) {
indexing_iterat or result ( *this );
++ my_base::the_in dex;
return ( result );
}

indexing_iterat or operator-- ( int ) {
indexing_iterat or result ( *this );
-- my_base::the_in dex;
return ( result );
}

indexing_iterat or operator+= ( typename my_base::differ ence_type dist ) {
my_base::the_in dex += dist;
return ( *this );
}

indexing_iterat or operator-= ( typename my_base::differ ence_type dist ) {
my_base::the_in dex -= dist;
return ( *this );
}

indexing_iterat or operator+ ( typename my_base::differ ence_type dist )
const {
return
( indexing_iterat or
( &my_base::the_c ontainer, my_base::the_in dex + dist ) );
}

indexing_iterat or operator- ( typename my_base::differ ence_type dist )
const {
return
( indexing_iterat or
( &my_base::the_c ontainer, my_base::the_in dex - dist ) );
}

};

template < typename C, bool b >
typename indexing_iterat or<C,b>::differ ence_type
operator- ( indexing_iterat or<C,bconst & lhs,
indexing_iterat or<C,bconst & rhs ) {
assert( lhs.the_contain er == rhs.the_contain er );
return ( lhs.the_index - rhs.the_index );
}

template < typename C, bool b >
bool operator== ( indexing_iterat or<C,bconst & lhs,
indexing_iterat or<C,bconst & rhs ) {
assert( lhs.the_contain er == rhs.the_contain er );
return ( lhs.the_index == rhs.the_index );
}

template < typename C, bool b >
bool operator!= ( indexing_iterat or<C,bconst & lhs,
indexing_iterat or<C,bconst & rhs ) {
assert( lhs.the_contain er == rhs.the_contain er );
return ( lhs.the_index != rhs.the_index );
}

template < typename C, bool b >
bool operator< ( indexing_iterat or<C,bconst & lhs,
indexing_iterat or<C,bconst & rhs ) {
assert( lhs.the_contain er == rhs.the_contain er );
return ( lhs.the_index < rhs.the_index );
}

template < typename C, bool b >
bool operator<= ( indexing_iterat or<C,bconst & lhs,
indexing_iterat or<C,bconst & rhs ) {
assert( lhs.the_contain er == rhs.the_contain er );
return ( lhs.the_index <= rhs.the_index );
}

template < typename C, bool b >
bool operator( indexing_iterat or<C,bconst & lhs,
indexing_iterat or<C,bconst & rhs ) {
assert( lhs.the_contain er == rhs.the_contain er );
return ( lhs.the_index rhs.the_index );
}

template < typename C, bool b >
bool operator>= ( indexing_iterat or<C,bconst & lhs,
indexing_iterat or<C,bconst & rhs ) {
assert( lhs.the_contain er == rhs.the_contain er );
return ( lhs.the_index >= rhs.the_index );
}
struct point {

typedef float value_type;

typedef value_type & reference;
typedef value_type const & const_reference ;
typedef value_type * pointer;
typedef value_type const * const_pointer;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type ;

value_type x, y, z;

point ( float x_, float y_, float z_ )
: x ( x_ )
, y ( y_ )
, z ( z_ )
{}

value_type const & operator[] ( std::size_t i ) const {
assert( i < 3 );
return ( (*this).*proxy( ) [i] );
}

value_type & operator[] ( std::size_t i ) {
assert( i < 3 );
return ( (*this).*proxy( ) [i] );
}

typedef indexing_iterat or<pointiterato r;

iterator begin ( void ) {
return ( iterator( *this, 0 ) );
}

iterator end ( void ) {
return ( iterator( *this, 3 ) );
}

typedef indexing_iterat or<point constconst_iter ator;

const_iterator begin ( void ) const {
return ( const_iterator( *this, 0 ) );
}

const_iterator end ( void ) const {
return ( const_iterator( *this, 3 ) );
}
private:

static
value_type point::* const * const proxy ( void ) {
static value_type point::* const dummy [3] =
{ &point::x, &point::y, &point::z };
return ( dummy );
}

};

namespace std {

template <>
struct less< point {

bool operator() ( point const & lhs, point const & rhs ) {
return ( std::lexicograp hical_compare( lhs.begin(), lhs.end(),
rhs.begin(), rhs.end() ) );
}

};

} // namespace std

#include <iostream>
#include <iomanip>
int main ( void ) {
point p ( 1, 1, 1 );
point q ( 1, 1, 1.1 );
std::cout << std::boolalpha << std::less<point >()( p, q ) << '\n';
p.x = q[2];
std::cout << p.x << '\n';
}
Just kidding :-)

However: hidden in the above code is yet another answer to your question.
Best

Kai-Uwe Bux
Sep 26 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
5512
by: (Pete Cresswell) | last post by:
I've got some SQL that works as far as returning a recordset from a series of UNION statements. viz: SELECT whatever UNION this UNION that UNION other
5
5232
by: Ricky Lung | last post by:
struct Foo { union { int& i; float& j; }; Foo(int& val) : i((int&)val), j((float&)val) {} };
6
3335
by: Neil Zanella | last post by:
Hello, I would like to know what the C standards (and in particular the C99 standard) have to say about union initializers with regards to the following code snippet (which compiles fine under gcc 3.2.2 but does not produce the expected results, the expected results being the ones annotated in the comments in the code): #include <stdlib.h>
16
344
by: Bun Head | last post by:
Does anyone see a problem with.. #include <stdio.h> typedef struct { float x; float y; float z; } _XYZ_;
10
11394
by: tapeesh | last post by:
I created a C file say struct.c with the following structure declarations in the same file struct A { union key { int i; float f; }k1;
56
10488
by: ccwork | last post by:
Hi all, Here is a sample code segment: .... typedef PACKED struct { union { PACKED struct { char red:1;
16
2111
by: tedu | last post by:
does anyone know of a platform/compiler which will place union elements to not overlap? as in union u { int a; long b; size_t c; }; in my limited experience, writing to any of (a, b, or c) will affect the value read from any other. i understand this is UB, but i'm
10
459
by: piboye | last post by:
Hi ! I'm a academician in china. I have been intereted in C++ lasting. In reading the C++ Primer book, i have a trouble about union. In the book ,it said that union can have constructors and destructor ,or other member functions. I can understand using constructors and memeber functions,but what is destructor used for? I have appealled to the forums in chinese ,but no enough usefull feedback.
30
3291
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10107
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.