0

I'm working on the graphics code for a game library in Java. I made package called com.engine.graphics. In this package, I have a lower-level class called VertexArrayObject. This class is used by "client-level" classes that clients will use; however, I do not want to give clients access to VertexArrayObject, since it would only serve to complicate their understanding of my library. Thus, I gave VertexArrayObject the default access specifier; that way, only classes within com.engine.graphicshave access to it, and also tells clients that they do not need to know what it is.

Just like there is this standard convention for Java, I figured there must be some standard convention for C++ for dealing with this; however, my internet searches have yielded no results.

So, my question is, what is the convention? And if there isn't one, what is the best approach?

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
hacksoi
  • 1,301
  • 13
  • 23
  • I don't think there is anything *identical*, but there are [***Friend classes***](https://en.wikipedia.org/wiki/Friend_class). – Elliott Frisch Mar 15 '16 at 03:06
  • 1
    Check out the [PIMPL Idiom](http://c2.com/cgi/wiki?PimplIdiom) – Austin Brunkhorst Mar 15 '16 at 03:09
  • This depends on exactly how the client classes use them. Can you be more specific? – Galik Mar 15 '16 at 05:43
  • @Galik, let's say there's a class ShapeRenderer in com.engine.graphics that clients use to draw basic shapes. This ShapeRenderer class uses VertexArrayObject in some way that does not concern clients; thus, I want the clients to be able to access ShapeRenderer but not VertexArrayObject. – hacksoi Mar 15 '16 at 06:02
  • Well that's doesn't tell me any more than you already have in the question. But, to be honest, your client programmers should only be programming to your published documentation. Anyone hunting round your header files using willy-nilly whatever they happen to find regardless what the documentation says is asking for trouble. They'll likely cause themselves problems whatever you do to try to save them from themselves. – Galik Mar 15 '16 at 06:21

1 Answers1

0

C++ does not have a notion of 'package' thus no 'package-protection' is technically possible. (There is a proposition for modules, but it will not be included even in the upcoming C++17 standard).

There are many ways of hiding the class from external world or restricting access to it, on syntax level you can resort to:

  • nested classes which may be declared private (you should be familiar with them from Java, except they are "static" by default and cannot access non-static enclosing class members) link;
  • friend classes that can access any private members of the given class link;
  • friend functions if you want to restrict access to only certain functions, including member functions of another class;
  • private/protected inheritance where only the class members are aware of the inheritance link.

Having a lot of friend classes and functions is a mess, but there is a reason for requiring explicit listing of those: they break the encapsulation principle.

Finally, you can use either "private implementation" idiom (aka pimpl, aka opaque pointer) that consists in defining a visible class holding a pointer to the implementation class and forwarding all calls while the implementation class is defined in a separate cpp file or the façade design pattern.

Chose whatever seems appropriate for the given class relation. Standard library tends to prefer nested classes while Qt, a popular graphic library, relies on pimpl.

ftynse
  • 787
  • 4
  • 9