+ 13

Is C++ the only language that has the concept of friend functions and classes?

Are there others?

6th Apr 2019, 4:27 AM
Sonic
Sonic - avatar
16 Respuestas
+ 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.
6th Apr 2019, 8:23 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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.
6th Apr 2019, 1:08 PM
Sonic
Sonic - avatar
+ 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
6th Apr 2019, 11:58 AM
Sonic
Sonic - avatar
+ 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
7th Apr 2019, 1:57 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
What about lua?
6th Apr 2019, 11:55 AM
[No Name]
[No Name] - avatar
+ 3
Yes, Only in C++ friend function and classes will exhibit .......
7th Apr 2019, 10:40 AM
YUGANDHAR SURYA
YUGANDHAR   SURYA - avatar
+ 2
Visual Basic also. C++ shares a lot of characteristics with VB
6th Apr 2019, 1:00 PM
Vasilis Rekatsinas
Vasilis Rekatsinas - avatar
+ 2
What is a friend function
6th Apr 2019, 1:13 PM
[No Name]
[No Name] - avatar
+ 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
6th Apr 2019, 1:28 PM
[No Name]
[No Name] - avatar
+ 1
I learned and I'm using lua with the roblox engine
6th Apr 2019, 1:30 PM
[No Name]
[No Name] - avatar
+ 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.
6th Apr 2019, 7:54 PM
Deud1eSkrub
Deud1eSkrub - avatar
+ 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.
6th Apr 2019, 7:58 PM
Deud1eSkrub
Deud1eSkrub - avatar
+ 1
No java also has a friend function.
7th Apr 2019, 5:57 PM
Manu Mishra
Manu Mishra - avatar
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++...
7th Apr 2019, 6:59 PM
Vasilis Rekatsinas
Vasilis Rekatsinas - avatar
0
No other languages like java is fully based on class concept.
9th Apr 2019, 7:39 PM
ALLWIN ANTO FRANKLIN .D
ALLWIN ANTO FRANKLIN .D - avatar