+ 13
Is C++ the only language that has the concept of friend functions and classes?
Are there others?
16 Respostas
+ 11
Yes. There is hardly any equivalent of C++'s 'friend' keyword.
http://www.stroustrup.com/bs_faq2.html#friend
The reason why 'friend' isn't popular among OO practitioners, and missing from many (if not all) languages, stems mainly from claims that 'friend' hurts encapsulation. Although I tend towards Bjarne's side that the claim is false, I really think that it has limited use cases, and the usage of friend functions and classes can be implemented in a more OO-conforming way, eliminating the need of 'friend'.
As it is right now, a friend function to a class has access level similar to that of a method which is internal to the class. If there really is such a function which needs to be friends with a larger number of classes, I would prefer to create a base class which holds the function as a public method, and for the other classes to inherit from this class. Not only would this be a better design in most cases, it also increases the portability of your code, given the lack of 'friend' in other languages.
+ 8
Thanks Vasilis Rekatsinas. I didn't even know VB was object oriented to start with. Good to know that VB has friends ;-) Gammelfleischvomtoasterschmecktgut I'm not sure if Lua has friend functions. Could you point me to a reference? BTW congratulations on having one of the longest usernames on Sololearn.
+ 7
Thanks for the detailed answer Hatsy Rei . For friend functions of a single class, I always thought that public getters and setters could achieve the same purpose. In fact, there was also another question in the SL Q and A section which asked about the differences between getters/setters and 'friend's. But I felt that no one had really answered that question. Here is the link.
https://www.sololearn.com/Discuss/1734098/?ref=app
+ 5
Vasilis Rekatsinas Sonic 'friend' in VB isn't equivalent to 'friend' in C++, it does nothing really close.
https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/friend
+ 3
What about lua?
+ 3
Yes, Only in C++ friend function and classes will exhibit .......
+ 2
Visual Basic also. C++ shares a lot of characteristics with VB
+ 2
What is a friend function
+ 2
Idk if you have no local before a function it can be accessed anywhere but with local it can be accessed by stuff after it I think when the local function is at end it can fire another function and give local variables and variables without local can be accessed anywhere but if you define functions or variables without local it will have some delay I think and if you change a variable with local it will effect the stuff after it
+ 1
I learned and I'm using lua with the roblox engine
+ 1
Lua itself isnât designed to be OO, but with metatables and metamethods its more powerful than python, at least in that sense, such as __index and __newindex can change the way a table(basically an array) behaves, allowing easy data management. So OOP is not all that bad with Lua.
+ 1
Also, Lua is made with C, so it interacts with it via the stack very heavy, so not specifying if itâs local doesnât mean anything can call it, youâd specify that by putting it in the global table(_G). With C or C++ you couldnât push something thatâs local to the top of the stack. Correct me if Iâm wrong.
+ 1
No java also has a friend function.
0
Hatsy Rei you are right. I know well enough VB, but I know very little about C++, so I was miss leaded... I think it is time starting a lesson in C++...
0
No other languages like java is fully based on class concept.