C结构体与C++结构体的区别
让我们讨论一下C和c++的结构体有什么不同?在c++中,结构体类似于类。
C和c++结构体的区别
| C结构体 | c++结构体 | 
|---|---|
| 只允许数据成员,不能有成员函数。 | 可以同时容纳:成员函数和数据成员。 | 
| 不能具有静态成员。 | 可以有静态成员。 | 
| 不能在结构中具有构造函数。 | 允许创建构造函数。 | 
| 不可能直接初始化数据成员。 | 可以直接初始化数据成员。 | 
| 写’ struct ‘关键字是必要的声明结构类型的变量。 | 声明结构类型变量不需要写’ struct ‘关键字。 | 
| 不要有访问修饰符。 | 支持访问修饰符。 | 
| 只允许指向结构体的指针。 | 可以同时具有指向结构体的指针和引用。 | 
| Sizeof操作符将为空结构体生成0。 | Sizeof操作符将为空结构体生成1。 | 
| 不可能隐藏数据。 | 数据隐藏是可能的。 | 
C和c++结构体的相似性
- 在C和c++中,默认情况下,结构的成员具有public可见性。
 
让我们来逐一讨论一下上面提到的一些异同:
结构体内部的成员函数
C语言中的结构体不能在结构体内部包含成员函数,但c++中的结构体可以在包含数据成员的同时包含成员函数。
// C Program to Implement Member
// functions inside structure
 
#include <stdio.h>
 
struct marks {
    int num;
 
    // Member function inside Structure to
    // take input and store it in "num"
    void Set(int temp) { num = temp; }
 
    // function used to display the values
    void display() { printf("%d", num); }
};
 
// Driver Program
int main()
{
    struct marks m1;
    // calling function inside Struct to
    // initialize value to num
    m1.Set(9);
 
    // calling function inside struct to
    // display value of Num
    m1.display();
}
输出:
This will generate an error in C but no error in C++. 
// C++ Program to Implement Member functions inside
// structure
 
#include <iostream>
using namespace std;
 
struct marks {
    int num;
 
    // Member function inside Structure to
    // take input and store it in "num"
    void Set(int temp) { num = temp; }
 
    // function used to display the values
    void display() { cout << "num=" << num; }
};
 
// Driver Program
int main()
{
    marks m1;
 
    // calling function inside Struct to
    // initialize value to num
    m1.Set(9);
 
    // calling function inside struct to
    // display value of Num
    m1.display();
}
输出:
num=9
静态成员
C结构体不能有静态成员,但c++允许有静态成员。
// C program with structure static member
 
struct Record {
    static int x;
};
 
// Driver program
int main() { return 0; }
// C++ program with structure static member
 
struct Record {
    static int x;
};
 
// Driver program
int main() { return 0; }
这会在C语言中产生错误,但在c++中不会。
在结构体中创建构造函数
C语言中的结构体不能在结构体中创建构造函数,但c++中的结构体可以创建构造函数。
// C program to demonstrate that
// Constructor is not allowed
 
#include <stdio.h>
 
struct Student {
    int roll;
    Student(int x) { roll = x; }
};
 
// Driver Program
int main()
{
    struct Student s(2);
    printf("%d", s.x);
    return 0;
}
// CPP program to initialize data member in c++
#include <iostream>
using namespace std;
 
struct Student {
    int roll;
    Student(int x) { roll = x; }
};
 
// Driver Program
int main()
{
    struct Student s(2);
    cout << s.roll;
    return 0;
}
这将在C语言中产生一个错误。
在c++中输出:
2
直接初始化
在C语言中不能直接初始化结构体数据成员,但在c++中可以这样做。
// C program to demonstrate that direct
// member initialization is not possible in C
 
#include <stdio.h>
 
struct Record {
    int x = 7;
};
 
// Driver Program
int main()
{
    struct Record s;
    printf("%d", s.x);
    return 0;
}
// CPP program to initialize data member in c++
#include <iostream>
using namespace std;
 
struct Record {
    int x = 7;
};
 
// Driver Program
int main()
{
    Record s;
    cout << s.x << endl;
    return 0;
}
这将在C语言中产生一个错误。
c++中输出:
7
Using struct关键字
在C语言中,我们需要使用struct来声明struct变量。在c++中,结构体不是必需的。
例如,有一个Record结构体。在C中,我们必须对Record变量使用“struct Record”。在c++中,我们不需要使用struct,只使用’ Record ‘就可以了。
访问修饰符
C结构体没有访问修饰符,因为语言不支持这些修饰符。c++结构体可以有这个概念,因为它是内置在语言中的。
指针和引用
在c++中,可以同时存在指向结构体的指针和引用,但在C中只允许指向结构体的指针。
sizeof操作符
该操作符在C语言中为空结构体生成0,而在c++中为空结构体生成1。
// C program to illustrate empty structure
 
#include <stdio.h>
 
// empty structure
struct Record {
};
 
// Driver Code
int main()
{
    struct Record s;
    printf("%lu\n", sizeof(s));
    return 0;
}
// C++ program to illustrate empty structure
#include <iostream>
using namespace std;
 
// empty structure
struct Record {
};
 
// Driver program
int main()
{
    struct Record s;
    cout << sizeof(s);
    return 0;
}
 
// This code is contributed by Shubham Sharma
C中的输出:
0
在c++中的输出:
1
注意:sizeof的默认类型是长unsigned int,这就是为什么在printf函数中使用” %lu “而不是” %d “的原因。
数据隐藏
C结构体不允许数据隐藏的概念,但在c++中是允许的,因为它是一种面向对象的语言,而C不是。
常量成员
C struct允许声明常量成员,但不允许初始化。但在c++中,你可以用 构造函数初始化器列表
#include <stdio.h>
 
struct C_struct
{
    const int i;
    int k;
};
int main()
{
    printf("Struct with constant members, but how to init??");
    return 0;
}
#include <iostream>
using namespace std;
 
struct Cpp_Struct
{
  public:
    const int i;
    int k;
    Cpp_Struct():i(2),k(3){}
};
int main()
{
    Cpp_Struct obj1;
    printf("Struct with constant members, %d, %d", obj1.i, obj1.k);
    return 0;
}
极客教程