Assume that i have written a program in C++ without using RTTI and run-time polymorphism (no virtual function, no virtual inheritance) and classes don't have private/protected members, also the C++ specific header files are not used (i.e. C header files are used: cstring
, cstdio
, ... instead of string
, iostream
, ...).
Then i want to write a similar program in C which the first argument type of the functions are corresponded to the related struct
.
For example:
//C++ code
struct Custom
{
int a;
Custom() { }
void change() { }
~Custom() { }
};
int main()
{
Custom m; //init m
m.change();
//destroy m
}
/*C code*/
struct Custom
{
int a;
};
void custom_init(Custom* like_this) { }
void custom_change(Custom* like_this) { }
void custom_destroy(Custom* like_this) { }
int main()
{
Custom m;
custom_init(&m);
custom_change(&m);
custom_destroy(&m);
}
Is the C++ program slower than the similar C program (Generally)? if yes, why C programs are faster then? I know, C++ uses the RAII design pattern for memory management, is it the reason for the slow?
I heard that some people said the C programs is faster... why?
Edit: Why this question is closed? i wanted to know if c++ does something additionally which we don't need, and how it affects the performance (makes it slower? faster? or nothing?).