美文网首页
2018-03-23

2018-03-23

作者: 时间煮鱼丶 | 来源:发表于2018-03-24 19:07 被阅读0次

1.struct内不能定义成员函数

include<stdio.h>

struct s
{
int x; //正确
void print(); //错误
};
int main()
{}
错误:字段‘print’声明为一个函数
void print();

2.struct成员默认为public, class成员默认为private

include<stdio.h>

include<string.h>

struct s
{
int x;
char name[20];
}s1;
int main()
{
s1.x=18;
strcpy(s1.name, "bzz"); //正确
printf("%s:%d\n", s1.name, s1.x);
}
输出:bzz:18

include<bits/stdc++.h>

using namespace std;

class s
{
int x;
}s1;

int main()
{
s1.x=5; //错误
}
报错:
test.cpp:6:6: 错误:‘int s::x’是私有的
int x;
^
test.cpp:11:5: 错误:在此上下文中
s1.x=5;

3.struct不可以设为空 class 可以

include<stdio.h>

include<string.h>

struct s
{
int x;
char name[20];
}s1;
int main()
{
struct s *s2=NULL; //错误
}
报错:
test.c: 在函数‘main’中:
test.c:10:9: 错误:无效的初始值设定
struct s s2=NULL;

4.class可以定义析构器,但是struct不可以

include<stdio.h>

include<string.h>

struct CTest
{
CTest();
~CTest(); //错误
int num;
};
int main()
{
}
报错:
test.c:5:5: 错误:expected specifier-qualifier-list before ‘CTest’
CTest();
^

  1. class可以有显式的无参构造器,但是struct不可以
    public struct structA
    {
    //public int A = 90; //错误:“structA.A”: 结构中不能有实例字段初始值
    public int A;
    //public structA() //错误:结构不能包含显式的无参数构造函数
    //{
    // this.A = 0;
    //}

    public structA(int paraA) //ok
    {
    this.A = paraA;
    }
    }

public class classA
{
public int A = 90;
public classA()
{
this.A = 90;
}
}

相关文章

  • 2018-03-24

    2018-03-23 静雪恋 2018-03-23 01:10 · 字数 289 · 阅读 0 · 日记本 打卡日...

  • python之os.path模块学习笔记

    author="jolting" date="2018-03-23" python os.path模块主要用于文件...

  • 2018-03-23

    2018-03-23 20:38 · 字数 367 · 阅读 0 · 日记本 【日精进打卡第64天/365天】 姓...

  • 搭建DNS服务器

    title: 搭建DNS服务器date: 2018-03-23 19:15:01tags: 前言 开发时拿到的开发...

  • 2018-03-27(稻盛哲学学习会)

    洪霞蔷薇 2018-03-23 23:29 · 字数 273 · 阅读 3 · 稻香湖 姓名:洪霞 部门:技术部 ...

  • 公关 案例

    危机公关|看Facebook和海底捞如何应对 中国MBA教育网 2018-03-23 23:28:45 Faceb...

  • 和谐的画面(17)

    文/紫玉姑娘 2018-03-23 亲爱的文文,这几天你终于和敏敏和谐相处了,你...

  • 那些所谓的爱情

    那些所谓的爱情 炮灰_5e54 2018-03-23 23:55 · 字数 911 · 阅读 0 · 日记本 那所...

  • 那些所谓的爱情

    那所谓的爱情 炮灰_5e54 2018-03-23 20:31 · 字数 881 · 阅读 9 · 日记本 刚开始...

  • EOS从头开始(一)——搭建环境

    本文是基于最新EOS稳定版2018-03-23安装,具体各版本可参见https://github.com/EOSI...

网友评论

      本文标题:2018-03-23

      本文链接:https://www.haomeiwen.com/subject/syffcftx.html