A<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B%2FClass%2FFriend</id>
		<title>C++/Class/Friend - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B%2FClass%2FFriend"/>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C%2B%2B/Class/Friend&amp;action=history"/>
		<updated>2026-04-17T03:46:44Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://cppe.ru/index.php?title=C%2B%2B/Class/Friend&amp;diff=1302&amp;oldid=prev</id>
		<title> в 14:21, 25 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C%2B%2B/Class/Friend&amp;diff=1302&amp;oldid=prev"/>
				<updated>2010-05-25T14:21:06Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 14:21, 25 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://cppe.ru/index.php?title=C%2B%2B/Class/Friend&amp;diff=1303&amp;oldid=prev</id>
		<title>Admin: 1 версия:&amp;#32;Импорт контента...</title>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C%2B%2B/Class/Friend&amp;diff=1303&amp;oldid=prev"/>
				<updated>2010-05-25T10:25:28Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия: Импорт контента...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Define friend function for ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;cstring&amp;gt;&lt;br /&gt;
#include &amp;lt;cstdlib&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class StringClass {&lt;br /&gt;
  char *p;&lt;br /&gt;
  int len;&lt;br /&gt;
public:&lt;br /&gt;
  StringClass(char *ptr);&lt;br /&gt;
  ~StringClass() {&lt;br /&gt;
     delete [] p;&lt;br /&gt;
  }&lt;br /&gt;
  friend ostream &amp;amp;operator&amp;lt;&amp;lt;(ostream &amp;amp;stream, StringClass &amp;amp;ob);&lt;br /&gt;
};&lt;br /&gt;
StringClass::StringClass(char *ptr)&lt;br /&gt;
{&lt;br /&gt;
  len = strlen(ptr)+1;&lt;br /&gt;
  p = new char [len];&lt;br /&gt;
  if(!p) {&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Allocation error\n&amp;quot;;&lt;br /&gt;
    exit(1);&lt;br /&gt;
  }&lt;br /&gt;
  strcpy(p, ptr);&lt;br /&gt;
}&lt;br /&gt;
ostream &amp;amp;operator&amp;lt;&amp;lt;(ostream &amp;amp;stream, StringClass &amp;amp;ob)&lt;br /&gt;
{&lt;br /&gt;
  stream &amp;lt;&amp;lt; ob.p;&lt;br /&gt;
  &lt;br /&gt;
  return stream;&lt;br /&gt;
}&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  StringClass stringObject1(&amp;quot;www.java2s.com&amp;quot;), stringObject2(&amp;quot;www.java2s.com&amp;quot;);&lt;br /&gt;
  cout &amp;lt;&amp;lt; stringObject1;&lt;br /&gt;
  cout &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; stringObject2 &amp;lt;&amp;lt; endl;&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
         &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==friend class for each other==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;   &lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class Curly &lt;br /&gt;
{&lt;br /&gt;
 public:&lt;br /&gt;
   Curly(char *msg) { strcpy(message, msg); };&lt;br /&gt;
   void show_message(void) { cout &amp;lt;&amp;lt; message &amp;lt;&amp;lt; endl; };&lt;br /&gt;
   friend class Moe;&lt;br /&gt;
   void show_moe(class Moe moe);&lt;br /&gt;
 private:&lt;br /&gt;
   char message[256];&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
class Moe &lt;br /&gt;
{&lt;br /&gt;
 public:&lt;br /&gt;
   Moe(char *msg) { strcpy(message, msg); };&lt;br /&gt;
   void show_message(void) { cout &amp;lt;&amp;lt; message &amp;lt;&amp;lt; endl; };&lt;br /&gt;
   friend class Curly;&lt;br /&gt;
   void show_curly(class Curly curly);&lt;br /&gt;
 private:&lt;br /&gt;
   char message[256];&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
void Curly::show_moe(class Moe moe) { cout &amp;lt;&amp;lt; moe.message &amp;lt;&amp;lt; endl; };&lt;br /&gt;
void Moe::show_curly(class Curly curly) { cout &amp;lt;&amp;lt; curly.message &amp;lt;&amp;lt; endl; };&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   class Moe moe(&amp;quot;nuck...&amp;quot;);&lt;br /&gt;
   class Curly curly(&amp;quot;whoop...&amp;quot;);&lt;br /&gt;
 &lt;br /&gt;
   moe.show_message();&lt;br /&gt;
   moe.show_curly(curly);&lt;br /&gt;
   curly.show_message();&lt;br /&gt;
   curly.show_moe(moe);&lt;br /&gt;
}&lt;br /&gt;
  &lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Friend function Demo==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class myclass {&lt;br /&gt;
  int num;&lt;br /&gt;
public:&lt;br /&gt;
  myclass(int x) { &lt;br /&gt;
     num = x; &lt;br /&gt;
  }&lt;br /&gt;
  friend int isneg(myclass ob);&lt;br /&gt;
};&lt;br /&gt;
int isneg(myclass ob)&lt;br /&gt;
{&lt;br /&gt;
  return (ob.num &amp;lt; 0) ? 1 : 0;&lt;br /&gt;
}&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  myclass a(-1), b(2);&lt;br /&gt;
  cout &amp;lt;&amp;lt; isneg(a) &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; isneg(b);&lt;br /&gt;
  cout &amp;lt;&amp;lt; endl;&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
         &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Friends can access private members of a class.==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using std::cout;&lt;br /&gt;
using std::endl;&lt;br /&gt;
class Count {&lt;br /&gt;
   friend void setX( Count &amp;amp;, int ); &lt;br /&gt;
public:&lt;br /&gt;
   Count() { x = 0; }                &lt;br /&gt;
   void print() const { cout &amp;lt;&amp;lt; x &amp;lt;&amp;lt; endl; }&lt;br /&gt;
private:&lt;br /&gt;
   int x;&lt;br /&gt;
}; &lt;br /&gt;
void setX( Count &amp;amp;c, int val )&lt;br /&gt;
{&lt;br /&gt;
   c.x = val;  // legal: setX is a friend of Count&lt;br /&gt;
}&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   Count counter;&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;counter.x after instantiation: &amp;quot;;&lt;br /&gt;
   counter.print();&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;counter.x after call to setX friend function: &amp;quot;;&lt;br /&gt;
   setX( counter, 8 );  // set x with a friend&lt;br /&gt;
   counter.print();&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
  &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Make the inserter into a friend function==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;cstring&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class PhoneNumber {&lt;br /&gt;
  char name[80];&lt;br /&gt;
  int areaCode;&lt;br /&gt;
  int prefix;&lt;br /&gt;
  int num;&lt;br /&gt;
public:&lt;br /&gt;
  PhoneNumber(char *n, int a, int p, int nm)&lt;br /&gt;
  {&lt;br /&gt;
    strcpy(name, n);&lt;br /&gt;
    areaCode = a;&lt;br /&gt;
    prefix = p;&lt;br /&gt;
    num = nm;&lt;br /&gt;
  }&lt;br /&gt;
  friend ostream &amp;amp;operator&amp;lt;&amp;lt;(ostream &amp;amp;stream, PhoneNumber o);&lt;br /&gt;
};&lt;br /&gt;
// Display name and phone number.&lt;br /&gt;
ostream &amp;amp;operator&amp;lt;&amp;lt;(ostream &amp;amp;stream, PhoneNumber o)&lt;br /&gt;
{&lt;br /&gt;
  stream &amp;lt;&amp;lt; o.name &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
  stream &amp;lt;&amp;lt; &amp;quot;(&amp;quot; &amp;lt;&amp;lt; o.areaCode &amp;lt;&amp;lt; &amp;quot;) &amp;quot;;&lt;br /&gt;
  stream &amp;lt;&amp;lt; o.prefix &amp;lt;&amp;lt; &amp;quot;-&amp;quot; &amp;lt;&amp;lt; o.num &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
  return stream; // must return stream&lt;br /&gt;
}&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  PhoneNumber a(&amp;quot;T&amp;quot;, 111, 555, 1234);&lt;br /&gt;
  PhoneNumber b(&amp;quot;A&amp;quot;, 312, 555, 5768);&lt;br /&gt;
  PhoneNumber c(&amp;quot;T&amp;quot;, 212, 555, 9991);&lt;br /&gt;
  cout &amp;lt;&amp;lt; a &amp;lt;&amp;lt; b &amp;lt;&amp;lt; c;&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
         &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Use friend function to access the non-public member variable==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class MyClass {&lt;br /&gt;
  int a, b;&lt;br /&gt;
  public:&lt;br /&gt;
    friend int sum(MyClass x);&lt;br /&gt;
    void set_ab(int i, int j);&lt;br /&gt;
};&lt;br /&gt;
void MyClass::set_ab(int i, int j)&lt;br /&gt;
{&lt;br /&gt;
  a = i;&lt;br /&gt;
  b = j;&lt;br /&gt;
}&lt;br /&gt;
int sum(MyClass object)&lt;br /&gt;
{&lt;br /&gt;
  return object.a + object.b;&lt;br /&gt;
}&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
  MyClass integer;&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Adding 3 and 4:&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
  integer.set_ab(3,4);&lt;br /&gt;
  cout &amp;lt;&amp;lt; sum(integer);&lt;br /&gt;
}&lt;br /&gt;
  &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>