M
Martin Vorbrodt
Hi there. Is this code portable between platforms? Is it also 100% standard
compliant?
#include <iostream>
using namespace std;
class Point {
public:
enum COORDS { X = 0, Y, Z };
Point(int x, int y, int z) : _x(x), _y(y), _z(z) {}
const int& operator[](COORDS c) const
{ return (&_x)[c]; }
private:
int _x, _y, _z;
};
int main() {
Point p(1, 2, 3);
cout << p[Point::X] << endl;
cout << p[Point::Y] << endl;
cout << p[Point::Z] << endl;
}
Is it save to get the address of the first member, and do pointer arithmetic
on it to get to all 3 elements?
Thanx
Martin
compliant?
#include <iostream>
using namespace std;
class Point {
public:
enum COORDS { X = 0, Y, Z };
Point(int x, int y, int z) : _x(x), _y(y), _z(z) {}
const int& operator[](COORDS c) const
{ return (&_x)[c]; }
private:
int _x, _y, _z;
};
int main() {
Point p(1, 2, 3);
cout << p[Point::X] << endl;
cout << p[Point::Y] << endl;
cout << p[Point::Z] << endl;
}
Is it save to get the address of the first member, and do pointer arithmetic
on it to get to all 3 elements?
Thanx
Martin