Skip to content

Last updated:2026-10-12

C++ struct

About the Author:

Personal WeChat official account:

wechat

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:

1
I am Base func

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:

struct Student {

};
//Student(){}
struct Student s; //ok
Student s;  //ok

After a function of the same name is added:

struct Student {

};
Student(){}
struct Student s; //ok
Student s;  //error

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;
}
For the complete code see: struct_func_func.cpp

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!

Comments