Jump to content
  • Advertisement
Sign in to follow this  
snooty

How private should classes be?

This topic is 4093 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm wondering, if I design a class in a static library that will only be used internally by other classes (not usable by the library users), should I declare all of its methods and constructors/destructor private (and be-friend-ing other internally classes so they can access it)?

Share this post


Link to post
Share on other sites
Advertisement
If the class is truly hidden (that is, the user never even needs to destruct it) then you can define it in a private header that the user shouldn't use (perhaps forward-declare it in a public header, if the user needs to manipulate it by pointer or reference).

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
If the class is truly hidden (that is, the user never even needs to destruct it) then you can define it in a private header that the user shouldn't use (perhaps forward-declare it in a public header, if the user needs to manipulate it by pointer or reference).


Seconded. Instead of saying "It's here, but only you, you, you, you and you can use it" to everyone, it's easier and more logical to only tell the relevant clients (i.e. your code) that it even exists.

Share this post


Link to post
Share on other sites
Thanks for the replies.

What is a "private header" exactly? Are there some preprocessor directives or something for it?

Share this post


Link to post
Share on other sites
Quote:
Original post by snooty
What is a "private header" exactly? Are there some preprocessor directives or something for it?


It's a header that's only included in library source files [smile]

EDIT: something I do to prevent anything evil is to set a 'COMPILING_MY_LIB' preprocessor defintion in my project settings or makefile when building the library, and adding the following at the top of my private headers right after the include guard:

#ifndef COMPILING_MY_LIB
# error This file should not be included outside my lib.
#endif


[Edited by - ToohrVyk on September 25, 2007 12:52:26 PM]

Share this post


Link to post
Share on other sites
Then when you distribute your library, you give your users the compiled library files and the headers they need. So not the private ones.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!