Last updated:2026-10-12
C++ struct¶
About the Author:¶
Personal WeChat official account:

1. struct in C¶
- In C, a struct is used purely as a composite data type; that is, only data members can be placed in the struct declaration, not functions.
- C struct declarations cannot use C++ access modifiers such as public, protected and private, whereas in C++ they can.
- In C, when defining a struct variable with the definition below, you must include the struct keyword.
- A C struct cannot be inherited (the concept does not exist).
- If a struct's name is the same as a function's name, the program still runs and calls normally! For example: you can define void Base() {} without conflicting with struct Base.
Complete example:
#include<stdio.h>
struct Base { // public
int v1;
// public: //error
int v2;
//private:
int v3;
//void print(){ // c中不能在结构体中嵌入函数
// printf("%s\n","hello world");
//}; //error!
};
void Base(){
printf("%s\n","I am Base func");
}
//struct Base base1; //ok
//Base base2; //error
int main() {
struct Base base;
base.v1=1;
//base.print();
printf("%d\n",base.v1);
Base();
return 0;
}
The final output:
For the complete code see: struct_func.c
2. struct in C++¶
Compared with C:
- In a C++ struct you can define not only data but also functions.
- A C++ struct can use access modifiers such as public, protected and private.
- A C++ struct can be used directly without the struct keyword.
- C++ inheritance
- If a struct's name is the same as a function's name, the program still runs and calls normally! But when defining a struct variable, you can only use the form with struct!
For example:
Case 1: defining a struct alias without typedef
Before a function of the same name is added:
After a function of the same name is added:
Case 2: defining a struct alias with typedef
typedef struct Base1 {
int v1;
int v3;
public: //显示声明public
int v2;
void print(){
printf("%s\n","hello world");
};
}B;
//void B() {} //error! 符号 "B" 已经被定义为一个 "struct Base1" 的别名
The first three cases
#include<iostream>
#include<stdio.h>
struct Base {
int v1;
// private: //error!
int v3;
public: //显示声明public
int v2;
void print(){
printf("%s\n","hello world");
};
};
int main() {
struct Base base1; //ok
Base base2; //ok
Base base;
base.v1=1;
base.v3=2;
base.print();
printf("%d\n",base.v1);
printf("%d\n",base.v3);
return 0;
}
For the complete code see: struct_func.cpp
Inheritance example
#include<iostream>
#include<stdio.h>
struct Base {
int v1;
// private: //error!
int v3;
public: //显示声明public
int v2;
virtual void print(){
printf("%s\n","Base");
};
};
struct Derived:Base {
public:
int v2;
void print(){
printf("%s\n","Derived");
};
};
int main() {
Base *b=new Derived();
b->print();
return 0;
}
For the complete code see: ext_struct_func.cpp
Same-name function
#include<iostream>
#include<stdio.h>
struct Base {
int v1;
// private: //error!
int v3;
public: //显示声明public
int v2;
void print(){
printf("%s\n","hello world");
};
};
typedef struct Base1 {
int v1;
// private: //error!
int v3;
public: //显示声明public
int v2;
void print(){
printf("%s\n","hello world");
};
}B;
void Base(){
printf("%s\n","I am Base func");
}
//void B() {} //error! 符号 "B" 已经被定义为一个 "struct Base1" 的别名
int main() {
struct Base base; //ok
//Base base1; // error!
base.v1=1;
base.v3=2;
base.print();
printf("%d\n",base.v1);
printf("%d\n",base.v3);
Base();
return 0;
}
3. Summary¶
Differences Between struct in C and C++¶
| C | C++ |
|---|---|
| Functions cannot be placed in a struct declaration | Functions can be placed in a struct declaration |
| C++ access modifiers cannot be used in a C struct declaration. | public, protected and private can be used in C++. |
| In C, when defining a struct variable with the definition above, you must include struct. | struct can be omitted |
| Structs cannot be inherited (the concept does not exist). | Inheritance is possible |
| If a struct's name is the same as a function's name, the program still runs and calls normally! | If a struct's name is the same as a function's name, to use the struct you can only use the definition with struct! |
Related Topics¶
- C++ Pure Virtual Functions and Abstract Classes | C++那些事
- C++ assert | C++那些事
- C++ Bit Fields | C++那些事
- Implementing C++ Polymorphism in C | C++那些事
- C++ const Constants and Qualifiers | C++那些事
- The Complete C++ const Family | C++那些事
- C++ Dereference Operator * | C++那些事
- C++ Initialization and Assignment | C++那些事
- Ways to Return Multiple Values in C++ | C++那些事
- C++ mutable Keyword | C++那些事