<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 一.友元 1. **功能** 讓其他類和函數訪問類的非公有成員。 2. **友元的聲明** 將函數或類或類的成員函數聲明放入類,并在聲明左邊加上 `friend` >[warning] 以下例子可在 除VC++6.0 外的編譯器下運行 ```c++ #include <iostream> using namespace std; class Complex { private: double real_; double imaginary_; public: Complex() : real_(0), imaginary_(0) { } Complex(double real) : real_(real), imaginary_(0) { } Complex(double real, double imaginary) : real_(real), imaginary_(imaginary) { } Complex(const Complex &old) { this->real_ = old.real_; this->imaginary_ = old.imaginary_; } Complex &operator=(const Complex &old) { this->real_ = old.real_; this->imaginary_ = old.imaginary_; return *this; } Complex &set_real(double new_val) { this->real_ = new_val; return *this; } Complex &set_imaginary(double new_val) { this->imaginary_ = new_val; return *this; } friend ostream &operator<<(ostream &os, Complex c);//友元函數的聲明 }; ostream &operator<<(ostream &os, Complex c) { return os << '(' << c.real_ << ',' << c.imaginary_ << ')'; } int main() { Complex c(2, 3); cout << c; return 0; } ``` >[test] >(2,3) 3. **VC++6.0友元函數BUG** + 當引入不帶 `.h` 的頭文件 且 使用了命名空間 std 時,將運算符重載函數作為友元函數將無法訪問類的私有成員。 + 解決方案 1. 使用帶 `.h` 的頭文件,此時不需要使用命名空間 `std` 2. 不要 `using namespace std` ,需要什么標識符就使用什么標識符 > 以下示例可在任意標準的C++中運行 ```c++ #include <iostream> //需要什么標識符就使用什么標識符 using std::ostream; using std::cout; class Complex { private: double real_; double imaginary_; public: Complex() : real_(0), imaginary_(0) { } Complex(double real) : real_(real), imaginary_(0) { } Complex(double real, double imaginary) : real_(real), imaginary_(imaginary) { } Complex(const Complex &old) { this->real_ = old.real_; this->imaginary_ = old.imaginary_; } Complex &operator=(const Complex &old) { this->real_ = old.real_; this->imaginary_ = old.imaginary_; return *this; } Complex &set_real(double new_val) { this->real_ = new_val; return *this; } Complex &set_imaginary(double new_val) { this->imaginary_ = new_val; return *this; } friend ostream &operator<<(ostream &os, Complex c); }; ostream &operator<<(ostream &os, Complex c) { return os << '(' << c.real_ << ',' << c.imaginary_ << ')'; } int main() { Complex c(2, 3); cout << c; return 0; } ``` >[test] >(2,3) 4. **好處** 靈活 5. **弊端** 破壞的類的封裝性
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看