结构变量查询

Struct Variable Query

本文关键字:查询 变量 结构      更新时间:2023-10-16
#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;
}

工作正常,输出

3
0
1
1

但在

#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;
}

我得到

3
0
68
68

我知道它未初始化,但为什么是 68?

如果在全局或内部函数中使用普通布尔值并在不初始化的情况下打印它 i 得到 0,那为什么不在这里呢?

并且有人也可以指出一些很好的来源来消除对此类变量声明,公共和私有概念,OOPS,结构和类之间的差异等的疑问。

[basic.fundamental]/6类型bool的值要么是true要么是false49

脚注49)以本国际标准描述为"未定义"的方式使用bool值,例如通过检查未初始化的自动对象的值,可能会导致其行为好像既不true也不false

未定义的行为是未定义的。这就是它的长短。讨论为什么一个表现出未定义行为的程序碰巧以特定的方式表现出来,这真的是毫无意义的。