-2
#include<bits/stdc++.h>
using namespace std;
typedef struct trie
{
    int arr[26];
    bool isleaf;
    trie(int isleaf)
    {
        this->isleaf=9;
        cout<<isleaf<<endl;
        isleaf=false;
        cout<<isleaf<<endl;
        cout<<this->isleaf<<endl;
    }
}* tr;
//void inser(s)
int main()
{
   tr k=new trie(3);
   cout<<k->isleaf;
}

Works Fine and outputs

3
0
1
1

But in

#include<bits/stdc++.h>
using namespace std;
typedef struct trie
{
    int arr[26];
    bool isleaf;
    trie(int isleaf)
    {
        cout<<isleaf<<endl;
        isleaf=false;
        cout<<isleaf<<endl;
        cout<<this->isleaf<<endl;
    }
}* tr;
//void inser(s)
int main()
{
   tr k=new trie(3);
   cout<<k->isleaf;
}

I get

3
0
68
68

I understand that it is uninitialized but still why 68?

If use a normal bool either in global or inside function and print it without initializing i get 0,then why not here?

And can somebody also point out some good source to clear doubts about such variable declarations , public and private concepts , and OOPS, difference between structs and classes etc.

mch
  • 8,070
  • 2
  • 24
  • 39

1 Answers1

0

[basic.fundamental]/6 Values of type bool are either true or false.49

Footnote 49) Using a bool value in ways described by this International Standard as “undefined,” such as by examining the value of an uninitialized automatic object, might cause it to behave as if it is neither true nor false.

Undefined behavior is undefined. That's the long and the short of it. It's really pointless to discuss why a program exhibiting undefined behavior happens to manifest it in a particular way.

Igor Tandetnik
  • 45,980
  • 4
  • 51
  • 75