0

Annoyance with C++: no elegant way to hide private members of a class while exposing its API.

Comments
  • 0
    (I know there are ways. Elegant is the key word.)
  • 0
    I'm not sure what you'd like to do? Do you have an example?
  • 0
    @willol You have a class with which you interact with public member functions. You need to define the class in a header file, which you publish for other files to use. But since you need to declare every member of the class right there, you need to show the private and protected members to the world.

    Ideally I would like to publish only the public parts in the header file (exposing only what needs to be exposed), and hiding the private and protected in the cop file.
  • 0
    @Gauthier ooookay, I never even thought about it! Yeah you're right
  • 0
    @Christos needs forward declaration, memory allocation... But maybe the least inelegant ;)
  • 0
    @Christos well in C you're forced to do OO with explicit pointers and anonymous struct, so you're less tempted to show private parts.

    It's the same as the opaque pointer thing in C++, but pointers are not very C++y (anymore), you need to deal with memory allocation which isn't very C++y anymore either, and you need to refer to these private parts through another object in the implementation.

    I find it OK in C, because that's how C is, pointery. I find it inelegant in C++ because it's less and less pointers. It feels like C with C++ syntax.
Add Comment