I know that this is detrimental to data hiding but theoretically is this allowed ?
1 Answer
Of course.
main
is an almost completely ordinary function.
-
8
main
is not completely ordinary. For instance, it may not be called from anywhere in code. Jul 14, 2011 at 15:27 -
3Not only
main
cannot be called from anywhere, it cannot be overloaded also.– NawazJul 14, 2011 at 15:35 -
3Friend declarations have the side effect of, well, being a declaration. That is, they do declare the function that you are befriended in the enclosing scope (or the fully qualified scope), so I thought that might be problematic. On the other hand, I have rechecked the standard, and what is forbidden is to define more than one
main
function (there is no mention to declarations), so it seems to be ok. At any rate, you will have to befriend all variants ofmain
that might be used in programs that use your class:int main()
,int main( int, char** )
,int main( int, char**, char** )
Jul 14, 2011 at 15:50 -
1@David, I've never encountered the last variant. What's the purpose of the second
char**
?– rcollyerJul 14, 2011 at 16:05 -
1@rcollyer it's
envp
, the environment variables, described in the C99 standard inJ.5.1/1
as a common extension.– CubbiJul 14, 2011 at 16:16