In my implementations, I should use a long array but my problem with array is that its indices do not much make sense to me. Instead I would like to use hierarchal classes. However, sometimes I need to treat them in a bulk way such as when calculating differences and derivatives or averages.
All members are double
and seems aligning does not make any problem. Here is an example as follows. This example apparently works fine.
My question is that is this structure of programming prone to failure on different compilers or systems?
#include <iostream>
class Room
{
public:
double size;
double temperature;
double humidity;
double oxigen_level;
// etc
};
class Kitchen
{
public:
double fan_speed;
double temperature;
};
class Building // a hierarchal class
{
public:
Room rooms[5];
double distance;
Kitchen kitchen;
};
Building diff(
const Building &b1,
const Building &b2) // treat as an array
{
Building r=b2;
double *p1=(double *)&b1;
double *pr=(double *)&r;
for(int i=0;i*sizeof(double)<sizeof(Building);i++)
pr[i]-=p1[i];
return r;
}
int main()
{
Building b1,b2,delta;
b1.rooms[3].humidity=0.44;
b2.rooms[3].humidity=0.43;
delta=diff(b1,b2);
std::cout
<<"diff: "
<<delta.rooms[3].humidity
<<std::endl;
return 0;
}