3 ms·
I used to know it very well. Now, not as much. Even when I wrote C++, I basically wrote "C, with classes."
by larrik 16y ago
I used to know it very well. Now, not as much. Even when I wrote C++, I basically wrote "C, with classes."
- ch0wn 16y agoThat is my favorite C++ style as well.
- cperciva 16y agoWhy write C-with-classes if you can just write C-with-structures-and-function-pointers instead?
- delinka 16y agoC-with-structures-and-function-pointers doesn't give you easy inheritance, polymorphism, etc. Now anthropomorphism ... that would be fun.
- swift 16y agoIt seems to me that it DOES pretty trivially give you polymorphism; just change the function pointers in a struct and it behaves differently, while retaining the same type! Inheritance can be implemented, too, by copying function pointers and other struct contents around. If you play with languages that use prototype-based OO like Lua and JavaScript, you can see pretty clearly how to implement such things. Implementing your own OO system in Lua is an experience I'd recommend for anyone who uses OO; it's easy, and you'll almost certainly learn something you can apply elsewhere.
- greyman 16y agoWhat would be the advantage of doing that?
- bediger 16y agoNo exceptions to deal with: http://ptgmedia.pearsoncmg.com/images/020163371x/supplements/Exception_Handling_Article.html http://ptgmedia.pearsoncmg.com/images/020163371x/supplements... That's probably a really big one in terms of reliability. Also, the weird C++ behavior of calling copy constructors in the most unexpected places can have a very detrimental effect on performance. Personally, I think the nature of C++ class definition leads people down the primrose path of trying to provide all potentially useful methods, of which only a tiny fraction get used. Tons of dead code appears in most C++ projects.
- dfox 16y agoFaster compilation, more flexibility, less dependencies in code base (you don't have to define struct fields in headers), no problems with incompatible ABI, interface that can be directly called from almost any other programing language...
- edan 16y agoIf you're a C purist (or just a purist in general) then of course you're right, but I personally think that C-with-classes is clearer, easier for a layman to understand (many people who call themselves C programmers still get nervous when function pointers come out to play), and is usually worth the trade-offs. You can write a lot of very decent code this way and have a little bit of "the best of both worlds", and avoid getting into a lot of the admittedly very dark corners and nasty surprises that C++ has to offer...
- larrik 16y agoIt's the same compiler, what's the advantage? I do want inheritance and such. Also, you may be giving up a lot of niceties in your IDE that way. Plus, if there's some feature of C++ I do like, I can bring it in. Ug, now it sounds like PHP. <3 Python (Edited the first line a bit)