F
familiewie
A quick example:
struct vec {
long d[2];
};
struct segment {
vec A, B;
};
struct polygon {
vec* points;
polygon(unsigned int size) { points=new vec[size]; }
~polygon() { delete[] points; }
};
int main() {
polygon test(4);
test.points[0][0] = 1; test.points[0][1] = 2;
test.points[1][0] = 3; test.points[1][1] = 4;
test.points[2][0] = 5; test.points[2][1] = 6;
test.points[3][0] = 7; test.points[3][1] = 8;
segment *border = reinterpret_cast<segment*>(test.points);
for(int i=0; i<3; ++i){
std::cout << '(' << border->A.d[0] << ", " << border->A.d[1] << "), (" << border->B.d[0] << ", " << border->B.d[1] << ')' << std::endl;
border = reinterpret_cast<segment*>(reinterpret_cast<char*>(border) + sizeof(vec));
}
}
This example works in Visual Studio. Does it also work with any other c/c++ compiler? Meaning is this usage of pointers, well defined in the c/c++ standards or is this plattform/compiler specific?
Thanks for any answer.
struct vec {
long d[2];
};
struct segment {
vec A, B;
};
struct polygon {
vec* points;
polygon(unsigned int size) { points=new vec[size]; }
~polygon() { delete[] points; }
};
int main() {
polygon test(4);
test.points[0][0] = 1; test.points[0][1] = 2;
test.points[1][0] = 3; test.points[1][1] = 4;
test.points[2][0] = 5; test.points[2][1] = 6;
test.points[3][0] = 7; test.points[3][1] = 8;
segment *border = reinterpret_cast<segment*>(test.points);
for(int i=0; i<3; ++i){
std::cout << '(' << border->A.d[0] << ", " << border->A.d[1] << "), (" << border->B.d[0] << ", " << border->B.d[1] << ')' << std::endl;
border = reinterpret_cast<segment*>(reinterpret_cast<char*>(border) + sizeof(vec));
}
}
This example works in Visual Studio. Does it also work with any other c/c++ compiler? Meaning is this usage of pointers, well defined in the c/c++ standards or is this plattform/compiler specific?
Thanks for any answer.