鱼C论坛

 找回密码
 立即注册
查看: 1072|回复: 3

[已解决]C++类对象的定义

[复制链接]
发表于 2017-7-5 20:14:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
在声明类时直接定义对象和在使用时定义对象为什么会有区别啊

直接定义

直接定义

使用时定义

使用时定义

在声明时定义对象,产生的都是0,而在使用时定义对象产生的是随机值??
这是C++规定吗?大佬们,
最佳答案
2017-7-6 16:41:41
用C语言举个例子吧
  1. #include <stdio.h>

  2. struct student
  3. {
  4.         int a, b, c, d;
  5. } little;


  6. int x;

  7. int main(void)
  8. {
  9.         printf("a: %d\n", little.a);
  10.         printf("b: %d\n", little.b);
  11.         printf("c: %d\n", little.c);
  12.         printf("d: %d\n", little.d);
  13.         printf("x: %d\n", x);
  14.        
  15.         return 0;
  16. }
复制代码

  1. a: 0
  2. b: 0
  3. c: 0
  4. d: 0
  5. x: 0

  6. --------------------------------
  7. Process exited after 0.2191 seconds with return value 0
  8. 请按任意键继续. . .
复制代码


全局变量如果没有给初始值,就初始化为0,没有疑问吧

那么
  1. class student
  2. {
  3. public:
  4.         int a, b, c, d;
  5. } little;
复制代码

这不就是声明并定义了一个全局的 little吗


第二个
  1. #include <stdio.h>

  2. struct student
  3. {
  4.         int a, b, c, d;
  5. };


  6. int main(void)
  7. {
  8.         struct student little;
  9.         int x;

  10.         printf("a: %d\n", little.a);
  11.         printf("b: %d\n", little.b);
  12.         printf("c: %d\n", little.c);
  13.         printf("d: %d\n", little.d);
  14.         printf("x: %d\n", x);

  15.         return 0;
  16. }
复制代码

编译时有警告
  1. 1>c:\visualstudioprojects\tmp\tmp\main.c(14): warning C4700: 使用了未初始化的局部变量“little”
  2. 1>c:\visualstudioprojects\tmp\tmp\main.c(18): warning C4700: 使用了未初始化的局部变量“x”
复制代码

很明显,没有初始化局部变量
在Visual Studio 2017中运行时报错
无标题.png
点忽略后可以输出
  1. a: -858993460
  2. b: -858993460
  3. c: -858993460
  4. d: -858993460
  5. x: -858993460
  6. 请按任意键继续. . .
复制代码


你的第二张截图不用说了吧,明显就是定义了一个局部变量,没有初始化
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-7-6 09:04:55 | 显示全部楼层
是不是C艹语言的机制这个回答不了你,但是一般都不会这样使用,正常都会使用构造函数进行赋初始值,正常int变量没有初值都是随机值,我觉得应该是编译器或者C艹本身的一个机制会将其置零
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-7-6 11:11:49 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-7-6 16:41:41 | 显示全部楼层    本楼为最佳答案   
用C语言举个例子吧
  1. #include <stdio.h>

  2. struct student
  3. {
  4.         int a, b, c, d;
  5. } little;


  6. int x;

  7. int main(void)
  8. {
  9.         printf("a: %d\n", little.a);
  10.         printf("b: %d\n", little.b);
  11.         printf("c: %d\n", little.c);
  12.         printf("d: %d\n", little.d);
  13.         printf("x: %d\n", x);
  14.        
  15.         return 0;
  16. }
复制代码

  1. a: 0
  2. b: 0
  3. c: 0
  4. d: 0
  5. x: 0

  6. --------------------------------
  7. Process exited after 0.2191 seconds with return value 0
  8. 请按任意键继续. . .
复制代码


全局变量如果没有给初始值,就初始化为0,没有疑问吧

那么
  1. class student
  2. {
  3. public:
  4.         int a, b, c, d;
  5. } little;
复制代码

这不就是声明并定义了一个全局的 little吗


第二个
  1. #include <stdio.h>

  2. struct student
  3. {
  4.         int a, b, c, d;
  5. };


  6. int main(void)
  7. {
  8.         struct student little;
  9.         int x;

  10.         printf("a: %d\n", little.a);
  11.         printf("b: %d\n", little.b);
  12.         printf("c: %d\n", little.c);
  13.         printf("d: %d\n", little.d);
  14.         printf("x: %d\n", x);

  15.         return 0;
  16. }
复制代码

编译时有警告
  1. 1>c:\visualstudioprojects\tmp\tmp\main.c(14): warning C4700: 使用了未初始化的局部变量“little”
  2. 1>c:\visualstudioprojects\tmp\tmp\main.c(18): warning C4700: 使用了未初始化的局部变量“x”
复制代码

很明显,没有初始化局部变量
在Visual Studio 2017中运行时报错
无标题.png
点忽略后可以输出
  1. a: -858993460
  2. b: -858993460
  3. c: -858993460
  4. d: -858993460
  5. x: -858993460
  6. 请按任意键继续. . .
复制代码


你的第二张截图不用说了吧,明显就是定义了一个局部变量,没有初始化
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 01:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表