initialization - Why I can use class Variable without initialize it c++ -
i have next code (make small & simple),
why did speed +=
working, although speed
wasn't initialized @ all?
#include <iostream> using namespace std; class vehicle { protected: int speed; public: virtual void repair(int j) { cout << "vehicle " << j << endl; if (repair()) { speed += j; cout << "speed:" << speed; } } int repair(){ cout << "vehicle repair " << endl; return 1; } }; void main() { vehicle v; //car c; citycar cc; vehicle * vp; //car * cp; vp = &v; vp->repair(1); }
in c++ can use uninitialized variables, @ you'll warning. except if set "warning errors" option, in case you'll error, obviously.
different compilers different things, in case, depending on compiler , compiler options, can warning, or not. see this question.
Comments
Post a Comment