-1

I have the next code (make it small & simple),

Why did I get the speed += working, although the speed wasn't initialized at 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);
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 2
    Speed just takes a junk value, so it might work but please initialize the variable first. It prevents errors. – Adrián Arroyo Calle Feb 05 '16 at 22:22
  • The question is why? if I will do: int i; i++; I will get an error – Alex Zemtzov Feb 05 '16 at 22:24
  • 2
    ***if I will do: int i; i++; I will get an error*** That should be a warning not an error unless you have enabled treat warnings as errors. – drescherjm Feb 05 '16 at 22:26
  • 2
    If you can do `void main()` then you can use uninitialized variables. BTW, the `main` function returns `int`. Always. – Thomas Matthews Feb 05 '16 at 22:28
  • the point is that I added the int i; i++; and I got error, but without it the speed is still get the junk value, what is the explanation for this? – Alex Zemtzov Feb 05 '16 at 22:28
  • 1
    I just did that added `int i; i++;` with Visual Studio 2013 and got `warning C4700: uninitialized local variable 'i' used` which is a warning not an error. I do not have treat warnings as errors enabled. – drescherjm Feb 05 '16 at 22:31
  • Error should not come. `gcc` also warning with -Wall. Else nothing – dlmeetei Feb 05 '16 at 22:32
  • What version of Visual Studio are you using? – drescherjm Feb 05 '16 at 22:37
  • Off topic. In GCC I like `-pedantic -pedantic-errors -Wall -Wextra -Werror` If you're gonna see 'em, you might as well fix 'em. – user4581301 Feb 05 '16 at 22:51
  • It's likely that the compiler doesn't know that `speed` is uninitialized at that point. Maybe it was initialized in another function, or even by a derived class. Since the compiler can't know, it assumes you know what you're doing. In the case of `int i; i++;`, it's pretty obvious that i is not initialized when you increment it. – Kevin Feb 05 '16 at 23:01
  • This isn't the problem, but `vp = &v; vp->repair(1);` can be written better as `v.repair(1);`. There's no need for a pointer here. – Pete Becker Feb 05 '16 at 23:34

3 Answers3

4

In C++ you can always use uninitialized variables, at most you'll get a warning. Except if you set a "warning as errors" option, in which case you'll get an error, obviously.

Different compilers do different things, so in your case, depending on the compiler and compiler options, you can get a warning, or not. See this question.

Community
  • 1
  • 1
Ilya
  • 5,377
  • 2
  • 18
  • 33
0

Note that in fact by creating an object pointer and then carry out an assignment operator on it you actually initialized it in junk value

try this:

Int x,*a;     
a=&x;
cout<<*a;
Israel.z
  • 321
  • 2
  • 6
0

Speed is a type of int, and int is a built-in type.If an object is of built-in type, then value-initializing it sets it to zero, and default-initializing it gives it an undefined value. In your case, speed has an undefined value. Adding this constructor with constructor initializer in public section should work for you:

Vehicle() : speed{0} {}

cppdisp
  • 41
  • 1
  • 8