r/cpp • • Jul 25 '24

Why use C over C++

Why there are so many people using the C language instead of C++?, I mean C++ has more Cool features and the Compiler also supports many CPUs. So why People still using C?

Edit: Thanks for all the usefull comments :D

235 Upvotes

444 comments sorted by

View all comments

Show parent comments

3

u/_Noreturn Jul 26 '24

functions in C can also hide very complex code. I do not get this

1

u/SmokeMuch7356 Jul 26 '24

Templates, overloaded operators, overloaded functions/methods, constructors, destructors, etc., con hide a lot of complexity that isn't obvious.

Yes, a function can hide some complexity, but you wouldn't expect an expression like x++ to invoke a lot of code, yet it can depending on what x is and how ++ has been overloaded.

5

u/_Noreturn Jul 26 '24 edited Jul 26 '24

templates do not hide code they are the same as functions.

for overloaded operators like == they are the exact same as their function counter parts I do not care as long it does its job conventionnally I do not want to lookup every function for how to compare each single struct instead of having a special named function for that


compare_my_string(&str1,&str2) == 0;  
 
my_string_iterator iter = my_string_get_begin(&str1);

while(iter != my_string_get_end(&str1)){
    my_iterator_dereference(iter);
    my_string_type_iterator_advance(&iter);
}
// vs
str1 == str2;
for(auto iter = str1.begin();iter != str1.end();iter++){
   *iter;
}

it is just C++ has a convention for these C functions in a uniform way called operator overloading

operator* to mean get / access operator== for comparing for equality opeator++ for advancing to the next value

etc...

if you do not follow these conventions then ofcourse I blame C++ instead of the developer who made operator== modify its left hand side. but wait I could have exactly made the equalivent C function called my_string_compare that modified the left arugement but you would not blame functions would you?

overloaded functions/methods,

overloaded functions are extra functions with 1 name and they should do something similar things if they do not I would blame the developer who created the API.

constructors, destructors

my_string_type str;
my_string_init(&str,"Hello World");
my_string_free(&str);
// vs
{
std::string str = "Hello World";
} // destructor called here

how is this hiding more than the C equalivent except the destructor call but it is when its lifetime ends and it does it automaticly

this is the exact same code just written on more lines and C code requires to explicitly remember to free it or you get the easiest memory leak in your life

I also do not care at all when my destructor is called and If need explicit control of it then I can do that also but 99% of the time I do not so why have the default in C being you have to remember to free stuff?

Yes, a function can hide some complexity

a function could hide the exact same amount of complexity in the equalivent C++