bate's blog

調べたこと実装したことなどを取りとめもなく書きます。

共用体2

個人的には、こっちの方が良いかな。
他の予約語にも、アッと驚く使い方があるのかしらん。

#include <iostream>

using namespace std;


/*
union Elements
{
	float m[4][4];

	struct
	{
		float m00, m01, m02, m03,
		   m10, m11, m12, m13,
		   m20, m21, m22, m23,
		   m30, m31, m32, m33;
	};
};
*/

class Matrix
{
public:
	union
	{
		float m[4][4];

		struct
		{
			float m00, m01, m02, m03,
			   m10, m11, m12, m13,
			   m20, m21, m22, m23,
			   m30, m31, m32, m33;
		};
	};
};

int main()
{
	Matrix mat;
	for( int i = 0; i < 4; ++i )
	{
		for( int j = 0; j < 4; ++j )
		{
			if( i != j )
				mat.m[i][j] = 0.0f;
			else
				mat.m[i][j] = 1.0f;
		}
	}

	cout << mat.m00 << " " << mat.m01 << " " << mat.m02 << " " << mat.m03 << endl;
	cout << mat.m10 << " " << mat.m11 << " " << mat.m12 << " " << mat.m13 << endl;
	cout << mat.m20 << " " << mat.m21 << " " << mat.m22 << " " << mat.m23 << endl;
	cout << mat.m30 << " " << mat.m31 << " " << mat.m32 << " " << mat.m33 << endl;

	cout << endl;

	for( int i = 0; i < 4; ++i )
	{
		for( int j = 0; j < 4; ++j )
			cout << mat.m[i][j] << " ";
		cout << endl;
	}
}