Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default constructor in C

Tags:

c

constructor

Is there a way to have some kind of default constructor (like a C++ one) for C user types defined with a structure?

I already have a macro which works like a fast initializer (like the one for pthread_mutex) but I wanted to know if you can by any chance have some (or all) fields of a struct filled at declaration.

For instance, with the pthread_mutex example, I would like

pthread_mutex_t my_mutex; 

to have the same effect as

pthread_mutex_t my_mutex = PTHREAD_MUTEX_INITIALIZER; 
like image 270
claf Avatar asked Feb 11 '09 15:02

claf


People also ask

What is a default constructor give an example?

Example 5: Default Constructor Hence, the Java compiler automatically creates the default constructor. The default constructor initializes any uninitialized instance variables with default values. In the above program, the variables a and b are initialized with default value 0 and false respectively.

What are the 3 types of constructor?

Constructors in C++ are the member functions that get invoked when an object of a class is created. There are mainly 3 types of constructors in C++, Default, Parameterized and Copy constructors.

What are the types of default constructor?

Default Constructors in C++ They are primarily useful for providing initial values for variables of the class. The two main types of constructors are default constructors and parameterized constructors. Default constructors do not take any parameters.

Why are default constructors used?

What is the significance of the default constructor? They are used to create objects, which do not have any specific initial value. Is a default constructor automatically provided? If no constructors are explicitly declared in the class, a default constructor is provided automatically by the compiler.


2 Answers

You can create initializer functions that take a pointer to a structure. This was common practice.

Also functions that create a struct and initialize it (like a factory) - so there is never a time where the struct is "uninitialized" in the "client" code. Of course - that assumes people follow the convention and use the "constructor"/factory...

horrible pseudo code with NO error checking on malloc or free

somestruct* somestruct_factory(/* per haps some initializer agrs? */) {   malloc some stuff   fill in some stuff   return pointer to malloced stuff }   void somestruct_destructor(somestruct*) {   do cleanup stuff and also free pointer   free(somestruct); } 

Someone will probably come along and explain how some early C++ preprocessors/compilers worked to do this all in C.

like image 186
Tim Avatar answered Sep 25 '22 04:09

Tim


C++ is different from C in this case in the respect that it has no "classes". However, C (as many other languages) can still be used for object oriented programming. In this case, your constructor can be a function that initializes a struct. This is the same as constructors (only a different syntax). Another difference is that you have to allocate the object using malloc() (or some variant). In C++ you would simlpy use the 'new' operator.

e.g. C++ code:

class A {   public:     A() { a = 0; }     int a; };  int main()  {   A b;   A *c = new A;   return 0; } 

equivalent C code:

struct A {   int a; };  void init_A_types(struct A* t) {    t->a = 0; }  int main() {    struct A b;    struct A *c = malloc(sizeof(struct A));    init_A_types(&b);    init_A_types(c);    return 0; } 

the function 'init_A_types' functions as a constructor would in C++.

like image 41
Reed Avatar answered Sep 24 '22 04:09

Reed