Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I've modified the text to address this comment:

EDIT: A lot of people point out that iterator should be used instead of pointer. However, imagine the object is contained in 10 different lists. You would have to pass structure containing 10 iterators around instead of the pointer. Morever, it doesn't solve the encapsulation problem, just moves it elsewhere. Instead of modifying "person" object every time you would want to add it to a new type of container you would have to modify the iterator tuple structure.



If the object is contained in 10 different lists, wouldn't the C version have 20 ptrs (10x prev and next)? That seems to me to be roughly equivalent complexity to your proposed "10x iterator" in C++, if I've understood correctly.

Basically, something needs to track your references if you have multiple lists. With C prev/next ptrs, the tracking is explicit in the object (well, the adjacent objects in the lists, to be more precise). With C++ containers, the tracking is iterator based.

With C++ you also have the option of using a smart pointer to do your usage tracking, which is probably simpler than either of the two approaches.


Yep. That's the way it is done in C. See Linux kernel for example. No problem with that as there's no expectation of well-encapsulated objects in C.


So you're saying one should use C over C++ because C++ doesn't provide an easy way do X and yet C doesn't even try? You seem to be complaining about a number of shortcomings in C++ and then preach that C is better for this even though the solutions you write in C have all the same shortcomings and more (not well-encapsulated). You're comparing apples to oranges. I don't quite understand the logic here at all.

The beauty of C++ is that you can use the features that you want and ignore the rest (and only pay for what you use). There is nothing stopping you from writing certain parts of your codebase in C-like ways where it makes sense and still benefit from other C++ language features.


Yes. For me the biggest added value of an OO model is the encapsulation. If a language is not capable of delivering it why use it at all?


The C version doesn't support having an object in multiple lists at all, so having it merely be awkward to have an object in multiple lists in the C++ version is still an advantage.




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: