double& operator [] (int i) { static double zero = 0.0; std::cout << "call& " << i << "zero = " << zero << std::endl; if(i<0) return zero; if(i>2) return zero; return (&_x)[i]; }
#include <iostream> #include <cassert> union Vec3 { public: Vec3() : _coord{0, 0, 0} { } Vec3(double x) : _coord{x, x, x} { } Vec3(double x, double y, double z) : _coord{x, y, z} { } double operator[](int i) const { assert(i >= 0); assert(i <= 2); return _tab[i]; } double& operator [] (int i) { assert(i >= 0); assert(i <= 2); return _tab[i]; } private: double _tab[3]; struct Coord { double x; double y; double z; } _coord; }; int main () { Vec3 v(1.1, 1.2, 1.3); std::cout << "v[0] : " << v[0] << std::endl; std::cout << "v[1] : " << v[1] << std::endl; std::cout << "v[2] : " << v[2] << std::endl; std::cout << std::endl; v[0] = 2.1; v[1] = 2.2; v[2] = 2.3; std::cout << "v[0] : " << v[0] << std::endl; std::cout << "v[1] : " << v[1] << std::endl; std::cout << "v[2] : " << v[2] << std::endl; std::cout << std::endl; v[3] = 3.4; double v3 = v[3]; std::cout << "v[3] : " << v[3] << std::endl; std::cout << " v3 : " << v3 << std::endl; std::cout << "v[4] : " << v[4] << std::endl; std::cout << std::endl; return 0; }
Vec3() { _coord.x = 0; _coord.y = 0; _coord.z = 0; } Vec3(double x) { _coord.x = x; _coord.y = x; _coord.z = x; } Vec3(double x, double y, double z) { _coord.x = x; _coord.y = y; _coord.z = z; }
Améliorer votre expérience CodeS-SourceS avec ce plugin:
ttp://codes-sources.commentcamarche.net/forum/affich-10000111-plugin-better-cs-2#cptpingu-signature
#include <iostream> #include <cassert> class Vec3 { public: Vec3() { _coord._suite.x = 0.0; _coord._suite.y = 0.0; _coord._suite.z = 0.0; } Vec3(double a) { _coord._suite.x = a; _coord._suite.y = a; _coord._suite.z = a; } Vec3(double a, double b, double c) { _coord._suite.x = a; _coord._suite.y = b; _coord._suite.z = c; } double operator[](int i) const { assert(i >= 0); assert(i <= 2); return _coord._tab[i]; } double& operator [] (int i) { assert(i >= 0); assert(i <= 2); return _coord._tab[i]; } private: union { double _tab[3]; struct { double x; double y; double z; } _suite; } _coord; }; int main () { Vec3 v(1.1, 1.2, 1.3); std::cout << "v[0] : " << v[0] << std::endl; std::cout << "v[1] : " << v[1] << std::endl; std::cout << "v[2] : " << v[2] << std::endl; std::cout << std::endl; v[0] = 2.1; v[1] = 2.2; v[2] = 2.3; std::cout << "v[0] : " << v[0] << std::endl; std::cout << "v[1] : " << v[1] << std::endl; std::cout << "v[2] : " << v[2] << std::endl; std::cout << std::endl; v[3] = 3.4; double v3 = v[3]; std::cout << "v[3] : " << v[3] << std::endl; std::cout << " v3 : " << v3 << std::endl; std::cout << "v[4] : " << v[4] << std::endl; std::cout << std::endl; return 0; }
v[0] : 1.1 v[1] : 1.2 v[2] : 1.3 v[0] : 2.1 v[1] : 2.2 v[2] : 2.3 v[3] : 3.4 v3 : 3.4 v[4] : 8.59626e-306
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionint main() { int i = 0; assert(++i == 1); std::cout << "Devrait être 1 == " << i << std::endl; return 0; }
std::vector<int> tab; // C++03 std::vector<int>::const_iterator end = tab.end(); for (std::vector<int>::const_iterator it = tab.begin(); it != end; ++it) std::cout << *it << std::endl; // C++11 for (int i : tab) std::cout << i << std::endl;