Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for the main function to be a friend of a class?

Tags:

c++

I know that this is detrimental to data hiding but theoretically is this allowed ?

like image 420
Srini Avatar asked Jul 14 '11 15:07

Srini


People also ask

Can main be a friend function?

main() can very well be a friend of any class. Just declare it as a friend inside the class like you do for other member functions.

Can we make main function as friend function in C++?

So a natural enforcement is to make the constructor private and main() as a friend. means a function named main() in namespace ns. So the real main() becomes irrelevant.

Can a function be friend of two classes?

A friend function can be friendly to 2 or more classes. The friend function does not belong to any class, so it can be used to access private data of two or more classes as in the following example. The friend functions can serve, for example, to conduct operations between two different classes.

Can a class be friend in C++?

Friend Keyword in C++You can use the friend keyword to any class to declare it as a friend class. This keyword enables any class to access private and protected members of other classes and functions.


1 Answers

Of course.

main is an almost completely ordinary function.

like image 158
SLaks Avatar answered Sep 23 '22 02:09

SLaks