-
July 7th, 2012, 07:55 PM
#1
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!
-
July 7th, 2012, 08:03 PM
#2
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.
-
July 8th, 2012, 04:02 AM
#3
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
-
July 8th, 2012, 01:08 PM
#4
Re: Public and private functions with no class?
Originally Posted by GCDEF
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!
Originally Posted by D_Drmmr
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!
-
July 8th, 2012, 03:52 PM
#5
Re: Public and private functions with no class?
Originally Posted by Leonhart231
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 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.
-
July 8th, 2012, 05:36 PM
#6
Re: Public and private functions with no class?
Originally Posted by monarch_dodra
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!
-
July 9th, 2012, 02:16 PM
#7
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.
-
July 10th, 2012, 10:04 AM
#8
Re: Public and private functions with no class?
Originally Posted by Lindley
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|