tags:

views:

642

answers:

9

I know C and Python, and I'm moving toward another language for learning purposes. My problem is that I like to learn things with something to do (for example contributing to some project or do something amazing, not boring plain algebra).

I would like to hear suggestions about the fields in which C++ shines and where I can find interesting programming with C++. (For fields I mean networking/GUI programming/algorithms/games ...)

I confirm that I'm interested in open source projects/development.

+3  A: 

In the OSS world (which I am assuming you are interested in), C++ is used a lot for GUI programming in KDE project (see Qt).

But my general observation is that C++ is used more in commercial applications (games, graphics-heavy applications), whereas in open-source projects C is preferred (in combination with some high-level language wrappers like Python).

Alex B
A: 

C++ is used for any field with high performance demands, as it has a valid reputation for being an extremely fast language. You'll find the very latest in technologies like DirectX and the very fastest algorithms available in C++.

DeadMG
That's funny. Try as I might, I just couldn't get DirectX working under g++ under my Ubuntu9 box :-)
paxdiablo
@paxdiablo: That's what the 'like' is for. Substitute in your platform's favourite graphics API at will. :D
DeadMG
@DeadMG and substitute C++ for the language of your choice.
Yacoby
@Yacoby Have you ever seen a DirectX binding for Lua? Writing such bindings is possible.. just nobody does it because they don't make sense. And, strangely enough, it's a C++ -> other language port. Such things don't exist natively for those languages.
DeadMG
I thought that the only C++ DirectX API was the managed one, which has been deprecated due to performance reasons (due to it being managed, not due to being C++). The functions in the DirectX 9 headers are all 'extern C', and like the rest of the Win32 API can be called just fine from C. Even if DirectX is implemented in C++, you don't need C++ to make use of it.
Pete Kirkham
No. Technically, you can use COM from C, but actually doing so is legendary for being difficult. COM is a C++ technology backported to C. The managed DirectX API is in .NET. If you #include <d3d9.h>, it comes with a slew of member functions and operator overloads. Compared to ptr->lpvtbl->function(ptr, args) in C, which is so verbose, they had to make a TYPE_FUNCTION(ptr, args) macro for every function call.
DeadMG
@DeadMG I don't see very many DirectX bindings, probably because OpenGL seems to be used more in FOSS. Almost every language I have seen has a OpenGL wrapper. For your information: http://luagl.luaforge.net/. They exist for Python, Haskell, Ruby...
Yacoby
@Yacoby: So you can use .. the far less commonly used game renderer .. by converting your language to C++. Not just that, but that doesn't make it actually good for that purpose.
DeadMG
@DeadMG I said nothing about converting anything to C++. You just call the C++ functions from the language of your choice.
Yacoby
@Yacoby: You didn't say it, but that's what such things are. Saying that DirectX/OGL aren't for C++ is like saying that the .NET framework isn't for Windows because Mono exists- sure, you can port it, but it ain't gonna work quite the same on the other end.
DeadMG
A: 

Most games are written in C++ because they are relatively low-level and need access to system APIs AND they profit from object-oriented programming a lot.

Alexander Gessler
I am not sure that that is true any more. Most of the time I think the game itself sits above a rendering/sound/physics API that abstracts all the low level APIs. It is also trivial to access things like the OpenGL api in something like Haskell
Yacoby
Games profit from being OOP? What?
jalf
@jalf, games don't profit from OOP? what?
Longpoke
Whilst it's true that many games are written in C++ - many in our industry are starting to review the extent to which C++ supports the game development architecture. Many studios are moving to a mixed model i.e C++ for gameplay/UI code but C/Assembler for systems and graphics code. However C++ does continue to be the language of choice in gamesdev for most studios
zebrabox
@Longpoke - Read http://research.scee.net/files/presentations/gcapaustralia09/Pitfalls_of_Object_Oriented_Programming_GCAP_09.pdf for some interesting ideas on this
zebrabox
@Longpoke: do they? More so than other software? OOP is no more or less relevant for games than for everything else. It is ridiculous to draw out game programming as an area where OOP is *especially* useful.
jalf
@Longpoke: and of course, *if* we assume that OOP is especially useful in games, then that is **not** an area where C++ shines. If you must have OOP at all costs in games, then you should stay miles away from C++. Write the game in C#, Smalltalk or some other language. There is no special relation between games and OOP, or between C++ and OOP
jalf
@jalf, Games, _like everything else_ (not necessarily "especially"), profit from OOP. @zebrabox: When you make a structure and a few functions that operate on that structure, this is OO... Nobody said you have to use dynamic dispatch. If you need to represent a person and all his attributes, you are going to either put that in a structure (C) or class (C++), no overhead is incurred here.
Longpoke
@Longpoke: The question was "where does C++ really shine". So you're saying that C++ *really shines* in the area of games development because games need **the same things as all other software**? Then C++ "really shines" at everything, doesn't it?
jalf
+9  A: 

I will share which fields I use the language in and why I use the language over others. Perhaps you can decide if my reasons qualify as 'shines'.

Which fields:

Device drivers, file system drivers, GUI development, algorithm modules, protocols and communications, application frameworks, data manipulation, storage handlers, system emulation.

Why:

  1. I want to write code that is portable across a broad scale of architectures. From small 16-bit embedded systems to large enterprise platforms. This is because I dislike solving the same problems over and over again. C++ compilers are available for more platforms I target than any other OO language. I do lose this capability on very very small (i.e. 8-bit) systems but I'm not spending much time in that space anymore.

  2. System code can be written (i.e. device drivers, FS drivers, etc.) as those require a language that compiles to native code. With careful selection of language features and libraries used it can be nearly as compact as C.

  3. Broad usage among compiled languages so there is peer experience to draw upon as well as available libraries and source code.

  4. Deterministic and predictable behavior over long execution runs (months to years) since the memory management scheme may be carefully selected for the application's needs.

  5. Acceptability to my clients. They are assured that the work is maintainable since a significant pool of developers exist in the market.

I hope that helped a little.

Amardeep
A: 

C++ means it can do something only C can do, and something else others can do.

When you want to see its shiny side, you can call it all-around. When you assume it's an useless language, you can take the answer at the bottom :-)

rhapsodyn
+11  A: 

In my opinion it's not much that C++ really shines, it's just I have yet to find a language that give me as much freedom.

  • C++ produces extremely tight and fast code, and therefore is suitable for most tasks, with the ability to seemlessly patch in some assembly code for the small parts that are not fast enough. Hooking C code into Python for performance is not nearly as easy.
  • C++ allows me to draw from multiple programming paradigms: metaprogramming, object-oriented programming, functional programming; they are available, I need not try to emulate any of them to get an algorithm to work.
  • C++ allows me to write exceptionally sound and maintanable code, drawing on the benefits of RAII (guaranteed disposal) and Pimpl (binary compatibility, compiler firewall) idioms.
  • C++ allows me to choose when I want to pay for something and when I'd rather not.
  • C++ allows me to work with many existing libraries, and draw on the experience and work. of dozens of thousands of programmers in the world. It requires some search as they are not bundled with the compiler, but Boost makes it easier these days, and then there is Google after all :)

There are drawbacks of course, the more freedom you are given the more likely you are to hang yourself (or shoot yourself in the foot, as the popular analogy goes). But after having been so free, I find it difficult to bring myself to program in another language.

C++ is, so far, the language in which I feel the less restricted, and thus the one I am the more comfortable programming with, despite its many flaws.

Matthieu M.
Nice overview. One minor complaint though: The way argument #2 is phrased suggests that C++ provides the means for *full-fledged* functional programming. I think a finer distinction should be made: Functional programming in C++ is only possible _to a certain extent_. For example, I don't know of any native syntax (at least not in standard C++ anno 1998) for doing things like partial function application, closures, pattern matching, defining lambda functions, etc. While that can probably be mimicked (see e.g. the STL and Boost libraries), I dare say these are no inbuilt features.
stakx
@stakx:Note that some of those features *are* built-in in the upcoming c++0x standard. And some of some of those features are already usable in contemporary compilers as extensions.
slacker
C++ also gives you an order of a magnitude more bugs and portability issues. C to Python bridging is as easy as typing a few lines in SWIG.
Longpoke
@slacker: lambdas are baked in in the up-coming C++0x.
Matthieu M.
@Longpoke: as long as you shy from compiler extensions, there should not be too much compatibility issues.
Matthieu M.
Regarding the popular analogy - I think it's C that lets you shoot yourself in the foot... C++ lets you blow off your whole leg, as I recall ;-)
Dan
+1  A: 

Intel and Nokia are united behind the smart device OS named MeeGo. And its main GUI framework is Qt. Which is a nice C++ API.

Gulshan
+4  A: 

IMO, the single biggest strength of C++ is being able to work at almost any level of abstraction. Many languages provide (more or less) a single level of abstraction. It's generally fairly difficult to "descend" to a lower level of abstraction (at all). While you can build up higher levels of abstraction, few languages provide the facilities to let those pieces work with (or like) the rest of the language. This can lead to "pieces" that are fine and sensible individually, but still difficult to put together into a complete program that remains coherent.

Nearly the only other language I can think of right off that has roughly similar capabilities in this respect is Ada 95. In some ways, its capabilities are (theoretically) superior. Just for example, it supports more precise specifications of things like bit layouts for interfacing to hardware devices. These remain mostly theoretical though -- while C++ (like C) lacks guarantees about how the compiler will lay out things like bit fields in a struct, individual compilers give the control necessary to do the job, and code like this is rarely very portable in any case.

Jerry Coffin
+1 for "able to work at almost any level of abstraction". This together with the fact that C++ is also a multi-paradigm language means that C++ is basically meant to be suitable for (almost) everything; and this is by design. As Bjarne Stroustrup (the inventor of C++) said himself: "Many C++ design decisions have their roots in my dislike for forcing people to do things in some particular way." -- Bjarne Stroustrup, in _The Design and Evolution of C++_, p. 23.
stakx
+1  A: 

For a little historical perspective, read Stroustrup's Design and Evolution of C++.

Stroustrup was working on a simulation back in his grad school days, and found Simula 67 to be an excellent language for the purpose, easy to write in.

However, Simula was completely inadequate for actual use, since it was so inefficient. Stroustrup was forced to rewrite the stuff in BCPL, which was a very primitive predecessor of C, and found it extremely painful.

After that, he was determined to come up with a language that was as effective as C, but which would support Simula-style programming. The result was at first called C with Classes, then C++.

However, C++ didn't stop there. It gained other features, like templates. This allowed the Standard Template Library. Its creator, Stepanov, claimed that he'd found no other language in which he could implement it to his satisfaction. (He seemed a bit grumpy about answering the question yet again in a Lisp forum.)

C++ shines at applying advanced and potentially weird programming techniques efficiently.

David Thornley