tags:

views:

248

answers:

4

First a little background ...

In what follows, I use C,C++ and Java for coding (general) algorithms, not gui's and fancy program's with interfaces, but simple command line algorithms and libraries.

I started out learning about programming in Java. I got pretty good with Java and I learned to use the Java containers a lot as they tend to reduce complexity of book keeping while guaranteeing great performance. I intermittently used C++, but I was definitely not as good with it as with Java and it felt cumbersome. I did not know C++ enough to work in it without having to look up every single function and so I quickly reverted back to sticking to Java as much as possible.

I then made a sudden transition into cracking and hacking in assembly language, because I felt I was concentrated too much attention on a much too high level language and I needed more experience with how a CPU interacts with memory and whats really going on with the 1's and 0's. I have to admit this was one of the most educational and fun experiences I've had with computers to date.

For obviously reasons, I could not use assembly language to code on a daily basis, it was mostly reserved for fun diversions. After learning more about the computer through this experience I then realized that C++ is so much closer to the "level of 1's and 0's" than Java was, but I still felt it to be incredibly obtuse, like a swiss army knife with far too many gizmos to do any one task with elegance. I decided to give plain vanilla C a try, and I quickly fell in love. It was a happy medium between simplicity and enough "micromanagent" to not abstract what is really going on. However, I did miss one thing about Java: the containers. In particular, a simple container (like the stl vector) that expands dynamically in size is incredibly useful, but quite a pain to have to implement in C every time. Hence my code currently looks like almost entirely C with containers from C++ thrown in, the only feature I use from C++.

I'd like to know if its consider okay in practice to use just one feature of C++, and ignore the rest in favor of C type code?

+4  A: 

Seems fine to me. That's the only part of C++ that I really use as well.

Ignacio Vazquez-Abrams
Interesting! So I'm not alone on this?
ldog
It's a far better use for C++ than just turning it into "C with classes".
Ignacio Vazquez-Abrams
+11  A: 

The short answer is, "This is not really the most effective way to use C++."

When used correctly, the strong type system, the ability to pass by reference, and idioms like RAII make C++ programs more likely to be correct, readable, and maintainable.

No one can stop you from using the language the way you want to. But you may be limiting yourself by not learning and leveraging actual C++ features.

If you write code that other people will have to read and maintain, they will probably appreciate the use of "real C++" instead of "C with classes" (in the words of a previous commenter).

bobbymcr
I agree, the encapsulation and class invariants offer an incredible way to actually reason about the complexity of your program. `struct` that have to be initialized and freed explicitly are an invitation to bugs.
Matthieu M.
+2  A: 

Right now, I'm writing a number cruncher. There's no polymorphism, no control delegation, no interaction. <iostream> was a bottleneck so I rewrote I/O in C.

The functions are mostly inside one class which represents a work thread. So that's not so much OO as having thread-local variables.

As well as vector, I use <algorithms> pretty heavily. But the heavy-duty data structures are written in plain C. Mainly circular singly-linked lists, which can't even easily have distinct begin() and end(), meaning not only containers but sequences (and for-loops) are off-limits. And then templates help the preprocessor to generate the main inner loop.

The most natural way of solving your problem is probably right. You don't want solutions in search of a problem. Learning to use C++ is well and good, but object orientation is suited to some problems and not others.

On the other hand, using bsearch from stdlib.h in a C++ program would be wrong.

Potatoswatter
+3  A: 

You should use C++ in whatever way makes the most sense for you.

John Dibling