Introduction to the use of C ++ Zhongyou Functions

Source: Internet
Author: User

Introduction to the use of C ++ Zhongyou Functions

1. youyuan Function

class A{  friend void MyFriend(class &A);private:  int m_a;}void MyFriend(class &A){   std::cout << A.m_a << std::endl;}

2. youyuan class

class A{public:  A();  friend class B;private:  int m_a;}class B{public:  B();  void UseA(A &a);}void B::UseA(A &a){  std::cout<< a.m_a << std::endl;}

3. Membership functions

// Pay attention to the Declaration Order to ensure that class A is visible; // A is used for pre-declaration, because class B {public: B () must be used in B (); void UseA (A & a);} class A {public: A (); friend void B: UsaA (A & a); private: int m_a;} void B :: useA (A & a) {std: cout <. m_a <std: endl ;}

4. Mutual friend functions (one function is a friend of two classes)

class B;class A{public:  A();  friend void UseAB(A &a,B&b);private:  int m_a;}class B{public:  B();  friend void UsaAB(A &a,B &b);private:  int m_a;}void UseAB(A &a,B &b){  std::cout << a.m_a + b.m_a << std::endl;}

Youyuan is an extension of the class interface, because only in the class can you specify who is youyuan, so, does not violate the OOP idea!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.