{"id":125,"date":"2009-07-24T11:46:41","date_gmt":"2009-07-24T10:46:41","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=125"},"modified":"2009-07-24T11:50:47","modified_gmt":"2009-07-24T10:50:47","slug":"friends","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/24\/friends\/","title":{"rendered":"Friends"},"content":{"rendered":"<p>A friend of a class allow access to the private members of that class, because there are some cases where this is required when the whole of the created class is required to be viewable, e.g. when a private member is not being able to set with the class methods.<\/p>\n<p>To declare a friend within a class, for example to declare a function \/ class as a friend.<\/p>\n<pre lang=\"cpp\">friend int FuncToFriendClass(int a);              \/\/ friend to the function.\r\nfriend class ClassToClassFriend;              \/\/ friend to a class<\/pre>\n<p>This will allow the function FuncToFriendClass to gain access to all objects within the class, and the class ClassToClassFriend as well.<\/p>\n<p>The source code<\/p>\n<pre lang=\"cpp\">\r\nint FuncToFriendClass(int a);              \/\/ declare the function, for the class to make a friend.\r\n\r\nclass FriendlyClass \r\n{\r\nprivate:\r\n       int private_data;\r\n\r\n       friend int FuncToFriendClass(int a);              \/\/ friend to the function.\r\n       friend class ClassToClassFriend;              \/\/ friend to a class\r\npublic:\r\n       FriendlyClass()\r\n       {\r\n              private_data = 5;\r\n       }\r\n};\r\n\r\nint FuncToFriendClass(int a)\r\n{\r\n       FriendlyClass friendClass;                            \/\/ the friend to this funciton.\r\n       return friendClass.private_data + a;\r\n}\r\n\r\nclass ClassToClassFriend\r\n{\r\npublic:\r\n       int FriendClassPrivateDataMinus(int b)\r\n       {\r\n              FriendlyClass classFriend;\r\n              return classFriend.private_data - b;\r\n       }\r\n};\r\n\r\nint main()\r\n{\r\n       ClassToClassFriend classFriend;\r\n       std::cout << \"Friendly function to class =\" << FuncToFriendClass(3) << \"\\n\";\r\n       std::cout << \"Friendly class to class = \" << classFriend.FriendClassPrivateDataMinus(2) << \"\\n\";\r\n       return 0;\r\n}\r\n<\/pre>\n<p>If you save that as friend.cpp and then compile, the output will be<br \/>\nFriendly function to class = 8<br \/>\nFriendly class to class = 3<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A friend of a class allow access to the private members of that class, because there are some cases where this is required when the whole of the created class is required to be viewable, e.g. when a private member is not being able to set with the class methods. To declare a friend within &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/24\/friends\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Friends<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[10,9,8,398,400],"class_list":["post-125","post","type-post","status-publish","format-standard","hentry","category-c_and_cpp","tag-c","tag-gcc","tag-gnu","tag-linux","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":3,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"predecessor-version":[{"id":128,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/125\/revisions\/128"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}