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

This seems to apply to just C++ actually.

Can I really do anything similar in C? I'm not sure. The tutorial makes use of overloading operators new and delete which (both operators and overloading feature) are only available in C++.

I think the title is misleading; as far as I know, term "C/C++" is a blanket and should be avoided.



There is basically nothing you can do in C but can't do in C++; the overloading is basically syntactical sugar. In fact, there are things you can explicitly do in C but can't do in C++ (at least if you insist on doing them the "C++ way"). Example: as far as I know, C++ doesn't let you use virtual constructors; C can implement this through function pointers.

Of course, if you treat C++ as a superset of C and just do things "the C way" in cases when "the C++ way" doesn't work, obviously C++ can do everything C can.


The lack of an equivalent of offsetof in C++ is a real pity. This effectively prohibits using C-style data containers, whereby container's control data is embedded into the data instance itself. This sort of thing:

  struct record
  {
    int foo;
    int bar;
    ...

    list_item item; // keeps 'record' on some list
    tree_item item; // keeps 'record' on some tree
  };
There's no concise and memory-efficient way of doing the same in C++.



> This seems to apply to just C++ actually.

why ? you can just as well create a cache of objects of a given type from within C, and use that for allocation/deallocation...


Well, you can replace malloc() and friends.




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

Search: