Function definition in C struct?

lulijeta

I have a C code I need to understand. There is a

typedef struct someStruct {
    int i; 
    char c; 
    someStruct() {
        i = 0;
        c = 'c';
    }
    someStruct(char inpChar) {
        i = 1;
        c = inpChar;
    }
} t_someStruct;

(The code doesn't really make sense or serve a purpose, I know. I just simplified it.) So there is this structure and it has two members (int i and char c). The interesting part is that it has basically two constructors, which is a new concept to me. It works normally, but can we write constructors for structures? I couldn't find anything on Google, maybe I am not searching right.

Basile Starynkevitch

Your code is not valid C code (i.e. valid C11) code but it is valid C++ (i.e. C++14) code.

In C++, a struct is like a class except that all members are by default public; see e.g. here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

c, function definition following struct

From Dev

Cryptic struct definition in C

From Dev

C struct definition standard

From Dev

Python ctypes definition for c struct

From Dev

C Declare the struct before definition

From Dev

C - struct definition in header files

From Dev

C: Incomplete definition of type struct

From Dev

C enum definition in struct with typedef

From Dev

C - struct definition in header files

From Dev

Definition of a function in a C program

From Dev

C function definition/prototype

From Dev

C function definition and declaration

From Dev

Declare pointer to a function that accepts a struct... from within the struct definition?

From Dev

Declare pointer to a function that accepts a struct... from within the struct definition?

From Dev

Purpose of Name before definition in C typedef struct

From Dev

Why is there a nested pointer inside a C struct definition?

From Dev

C++ Enum in struct error for definition

From Java

C - function inside struct

From Dev

C function and struct on the fly

From Dev

function definition in BNF C grammar

From Dev

Find function definition in C++

From Dev

Weird declaration and definition of a function in C

From Dev

Elegant function definition in C++

From Dev

Find function definition in C++

From Dev

function definition in BNF C grammar

From Dev

Elegant function definition in C++

From Dev

Where should be the definition of a struct be declared in C? .c or .h?

From Dev

C - Mismatched function prototype and definition for "static" function

From Dev

Is the 'main' function classified as a function definition in C?