learning C with a C++ background

S

Stephen Sprunk

Jonathan Mcdougall said:
But is this what I want to do? Do I want to emulate classes in C?

Not in general, no. You pointed out Gtk as seeming cumbersome, and the
reason for that is that Gtk is basically C++ translated (mentally) to C.

As the saying goes, "You can write FORTRAN in any language." That doesn't
mean doing so is a good idea. If OOP is what you need, you already know
C++. If it's not what you need, don't import it and force C into a mold it
doesn't fit -- do things the C way instead.
But I find it lacks important several features.

Remember that C90 was basically an attempt to document what already existed
at the time; very, very few new features were added to the language. C
evolved, whereas C++ had an "intelligent designer" (only US residents are
likely to catch that reference).
I searched a while for an "STL" equivalent (containers and algorithms).
What do people use when they need a linked list? Are there
commonly used libraries available, such as boost for C++?

There are common implementations of many of the things you're looking for,
but common practice is to write your own as an integral part of the code.
Need a vector<>? Use an array. Need a list<>? Add a next (and possibly
prev) pointer to your structs. Need an iterator? Use a pointer of the same
type. etc. "Generic" implementations add a lot of overhead; if we wanted
to pay that cost, we'd go use C++ instead of C.

The biggest thing you need to learn is how different memory management is,
i.e. malloc() and free() instead of new and delete. "Pass by reference" in
C lingo means what you think of as "pass by pointer"; there's no equivalent
to C++'s references. And, of course, strings are totally different since
you do the memory management yourself instead of letting some class handle
it for you.

The only reason I can see someone wanting to move from C++ to C is to get
closer to the metal and get rid of all the OOP overhead. If you don't look
at it in that light, a lot of C won't make sense to you. Each language has
its strengths; trying to solve problems that C++ is good at in C will not
make sense, so try to figure out what problems C++ is bad at and examine why
C solves them more efficiently. Then you can choose which to use for any
given problem.

S
 
I

Ian Collins

Stephen said:
The only reason I can see someone wanting to move from C++ to C is to
get closer to the metal and get rid of all the OOP overhead

I don't think either of these are reasons, C++ gets you just a close to
the metal and you don't have any OOP overhead if you don't use OOP.

The most common reason is lack of a C++ compiler for the target platform.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top