Public and private functions with no class?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Public and private functions with no class?

  1. #1
    Join Date
    Jul 2012
    Posts
    3

    Public and private functions with no class?

    I've been writing my own implementation of quicksort in a .h file. I've gotten to the point where I think I'm done with the algorithm, so now I'm trying to make it so that if someone includes "quicksort.h", they will only have access to certain functions in the file. Kind of like public and private functions in a class, but without the "class" part of it. How do you do that? And if there's any other information you need, let me know!

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,978

    Re: Public and private functions with no class?

    I would think the best approach is just to use two header files and only distribute the one with the public functions.

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    1,998

    Re: Public and private functions with no class?

    If your quicksort function is not a template, you can put the implementation in a cpp file. The "private" functions can be placed inside an anonymous namespace to prevent anyone from using them outside the cpp file.
    If you have a template function, then code that uses it will need to see the implementation, so the only thing you can do it put the "private" functions inside a namespace (e.g. "detail") that indicates that they are implementation details.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  4. #4
    Join Date
    Jul 2012
    Posts
    3

    Re: Public and private functions with no class?

    Quote Originally Posted by GCDEF View Post
    I would think the best approach is just to use two header files and only distribute the one with the public functions.
    Alright, I can see this working well, so now I just need to figure out how to properly distribute .h's so I don't lose the "private" functions. Thank you!

    Quote Originally Posted by D_Drmmr View Post
    If your quicksort function is not a template, you can put the implementation in a cpp file. The "private" functions can be placed inside an anonymous namespace to prevent anyone from using them outside the cpp file.
    If you have a template function, then code that uses it will need to see the implementation, so the only thing you can do it put the "private" functions inside a namespace (e.g. "detail") that indicates that they are implementation details.
    They are template functions, and unfortunately, I'd rather not use a namespace since that apparently (I only have 2 quarters of university C++ under my belt) stops the user from using a different namespace (but correct me if I'm wrong). Also, if I did do what you suggested, would the "private" functions still need their own .cpp, or would it all be in the single .h? Thanks!

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,275

    Re: Public and private functions with no class?

    Quote Originally Posted by Leonhart231 View Post
    They are template functions, and unfortunately, I'd rather not use a namespace since that apparently (I only have 2 quarters of university C++ under my belt) stops the user from using a different namespace (but correct me if I'm wrong). Also, if I did do what you suggested, would the "private" functions still need their own .cpp, or would it all be in the single .h? Thanks!
    If they are templates, then everything goes into .h files. There will be no .cpp file, unless you have parts that don't depend on a parametrized type.

    The standard thing to do is usually to put your "public" methods where you want them to be (either mainspace, or a library namespace), and then to put the "private" methods inside a sub namespace called "implementation" or "details" or similar. This means the client will not be bothered by your private functions, or accidentally call them.

    This is usually good enough, as calling your private methods can only be done deliberately and with full knowledge of doing something wrong. At this point, you can't stop them from doing what they want, and the behavior of their program seizes to be your problem anyways.
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  6. #6
    Join Date
    Jul 2012
    Posts
    3

    Re: Public and private functions with no class?

    Quote Originally Posted by monarch_dodra View Post
    If they are templates, then everything goes into .h files. There will be no .cpp file, unless you have parts that don't depend on a parametrized type.

    The standard thing to do is usually to put your "public" methods where you want them to be (either mainspace, or a library namespace), and then to put the "private" methods inside a sub namespace called "implementation" or "details" or similar. This means the client will not be bothered by your private functions, or accidentally call them.

    This is usually good enough, as calling your private methods can only be done deliberately and with full knowledge of doing something wrong. At this point, you can't stop them from doing what they want, and the behavior of their program seizes to be your problem anyways.
    Ah, excellent! That worked perfectly, and it's a little cleaner than a second .h. Thanks for the help!

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: Public and private functions with no class?

    If this is an assignment, cool. For future reference, however, be aware that it is very difficult to do better than the standard library's std::sort() method.

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,638

    Re: Public and private functions with no class?

    Quote Originally Posted by Lindley View Post
    If this is an assignment, cool. For future reference, however, be aware that it is very difficult to do better than the standard library's std::sort() method.
    Might be better to say
    it is very difficult to do better than the standard library's std::sort() method as a general purpose sorter.

    For specific cases, you can always come up with something better.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center