OOP doesn’t imply using function pointers.
The essence of OOP is polymorphism, which you can achieve in C through function pointers.
What about just having a plain structure and associate functions to it ? This is OO. In C, it forces you to adopt conventions like prefixing all your function names with the class name,…
I assume you’re refering to encapsulation, and I don’t see having to adopt a convention of prefixing your function with a object name (you’re not forced to) is a big deal unless you hunt and peck.
…and explicitely dereference all the member variables whenever you access them.
I assume you’re referring to having to pass in a pointer to structure and then using -> to dereference within a function. I don’t see how that’s a problem.
What about access rights ? There’s no way in C to forbid access to certain members of a structure. You have to document them in some way, by adding a comment next to the mmbers you consider private. And its not enforced by the compiler, thus errors can creep up.
Programmers should be using the public API. having public, protected, and private is no silver bullet.
Yes, programming using interface is useful, but there are lots of cases where objects with zero runtime overhead, but that implicitely do various housekeeping stuff, are very useful. Or even, classes that are there only to keep your code from getting full of long and messy functions, but amount to nothing at runtime.
Interfaces with virtual functions have a runtime cost, and these functions can’t be inlined. They’re not a silver bullet.
You don’t pay for what you don’t use in C also…unlike Java where you’re relying on the runtime to be smart about all methods being virtual.
And what about error management anyway ? You have to think of freeing resources everywhere, and C has no mechanism whatsoever to help you to do it. I don’t think it’s much better than an exception system, even if it has not so hard to avoid pitfalls.
Exceptions are useful, even though it has been pointed out that C++ exceptions have some major warts.
I understand your point, but I don’t think it’s really C++ fault. It’s more that since it allow to do these things more easily, it’s tempting to over-complicate stuff. I myself prefer straightforward code where I know what each classes actually do, and not have a vast amount of layers of abstraction if I don’t need them.
Yes, you can do everything in C that you do in C++, like object programming and stuff.
The purpose of C++ isn’t to let you do things that can’t be done in C, it’s to simplify your life. Indeed, it can complicate it instead, but not if used properly.
Pretty much agree on these points. Shallow hierarchies and favoring composition are the way to go.
I’ve written lots of C++ code in my lifetime (more than any other language). I think I somewhat grok proper OO. I’ve got Stroustrup’s 3rd edition I’ve got the GOF Design Patterns book, and think patterns are useful.
I guess my biggest problem with C++ is the syntax, the overcomplification of the language that seems to stem from it’s legacy of needing to be C-backward compatible, can’t stand C++ streams, and think much of the standard library API is bad.