0

When I tried to run the code below, I got totally confused..

#include<iostream>
using namespace std;

class test{
    public:
    int num = 30;

    int yourAge ;

    void set(){
        yourAge = (test().num)*2; 
    }
};

int main(){
    test obj;


    cout << test().yourAge << endl; // i was expecting garbage but giving me 0
    cout << obj.yourAge << endl;    // ya! garbage it is, in this case

    ///// now/////

    test().set()
    cout << test().yourAge << endl;  // Iwas expecting 60 but giving me againg 0

    cout << obj.yourAge << endl;  // I was expecting garbage but giving me 0;



    return 0;
}

I have described my problems in the code, but why they are giving unexpected results. Why that is happening? Does it because I am using temporary nameless object or something else is happening? I will be thankful for your answers.

sparsh goyal
  • 89
  • 1
  • 7
  • 1
    Stop right there. You are designing tests based on undefined behavior and expecting some kind of predictable result. – paddy Jul 19 '19 at 05:37
  • 1
    When you access the value of an object that is uninitialized, don't expect any sensible behavior. – R Sahu Jul 19 '19 at 05:38
  • 13
    Sometimes zero is garbage. Sometimes garbage is zero. – user4581301 Jul 19 '19 at 05:39
  • I don't think you'll gain much from digging this any further. – Dean Seo Jul 19 '19 at 06:09
  • Undefined behavior is not required to behave. – Öö Tiib Jul 19 '19 at 06:09
  • 1
    When you call `test().set()` and then expect that `test().yourAge` changed somehow is wrong because in each case new temporary object is created and destroyed at end of expression. There is no connection whatsoever between different temporaries except for case with static variables. – sklott Jul 19 '19 at 06:11
  • Check out this question: https://stackoverflow.com/questions/29765961/default-value-and-zero-initialization-mess. Behaviors can be different depending on how you initialize the object and which c++ standard you are using. – uNiverselEgacy Jul 19 '19 at 06:47
  • I suggest you have a look at: https://stackoverflow.com/questions/2417065/does-the-default-constructor-initialize-built-in-types – caiomcg Jul 31 '19 at 14:34

1 Answers1

4

Anywhere you use test() you build a temporary object of type test which is initialised as specified in your class.
This initialisation is 30 for the num member but could be anything for the yourAge member (except with static storage but it's another topic).

Then any expression like test().yourAge builds a temporary object, retrieves its yourAge member which value is uninitialised (it could be -17, 0 or whatever) and lets this temporary object disappear.

When calling test().set() you create a temporary object, initialise its yourName member thanks to the set() method but let this object disappear without using it!
Any subsequent test() expression is a new temporary object with no reason to be related to the one on which set() was called.

The case of obj is similar except that this object will last as long as the block containing it (the main() function here) is executed.
Its yourAge member is never initialised, so it could contain anything; if you ever used obj.set() then, after that call you could have expected to find something relevant in obj.yourAge.

prog-fh
  • 13,492
  • 1
  • 15
  • 30