• <bdo id='9JYIp'></bdo><ul id='9JYIp'></ul>
  • <tfoot id='9JYIp'></tfoot>

      <small id='9JYIp'></small><noframes id='9JYIp'>

      <legend id='9JYIp'><style id='9JYIp'><dir id='9JYIp'><q id='9JYIp'></q></dir></style></legend>
      <i id='9JYIp'><tr id='9JYIp'><dt id='9JYIp'><q id='9JYIp'><span id='9JYIp'><b id='9JYIp'><form id='9JYIp'><ins id='9JYIp'></ins><ul id='9JYIp'></ul><sub id='9JYIp'></sub></form><legend id='9JYIp'></legend><bdo id='9JYIp'><pre id='9JYIp'><center id='9JYIp'></center></pre></bdo></b><th id='9JYIp'></th></span></q></dt></tr></i><div id='9JYIp'><tfoot id='9JYIp'></tfoot><dl id='9JYIp'><fieldset id='9JYIp'></fieldset></dl></div>

        什么是 C++ 中的代理类

        时间:2023-10-07
          • <bdo id='rj44D'></bdo><ul id='rj44D'></ul>

                <i id='rj44D'><tr id='rj44D'><dt id='rj44D'><q id='rj44D'><span id='rj44D'><b id='rj44D'><form id='rj44D'><ins id='rj44D'></ins><ul id='rj44D'></ul><sub id='rj44D'></sub></form><legend id='rj44D'></legend><bdo id='rj44D'><pre id='rj44D'><center id='rj44D'></center></pre></bdo></b><th id='rj44D'></th></span></q></dt></tr></i><div id='rj44D'><tfoot id='rj44D'></tfoot><dl id='rj44D'><fieldset id='rj44D'></fieldset></dl></div>

                  <tfoot id='rj44D'></tfoot>
                1. <small id='rj44D'></small><noframes id='rj44D'>

                    <tbody id='rj44D'></tbody>
                  <legend id='rj44D'><style id='rj44D'><dir id='rj44D'><q id='rj44D'></q></dir></style></legend>

                  本文介绍了什么是 C++ 中的代理类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  什么是 C++ 中的代理类?为什么创建它以及它的用处是什么?

                  What is a Proxy Class in C++? Why it is created and where it is useful?

                  推荐答案

                  代理是为另一个类提供修改后的接口的类.

                  A proxy is a class that provides a modified interface to another class.

                  这是一个例子 - 假设我们有一个数组类,我们只想包含二进制数字(1 或 0).这是第一次尝试:

                  Here is an example - suppose we have an array class that we only want to contain binary digits (1 or 0). Here is a first try:

                  struct array1 {
                      int mArray[10];
                      int & operator[](int i) {
                        /// what to put here
                      }
                  }; `
                  

                  我们希望 operator[] 在我们说类似 a[1] = 42 时抛出,但这是不可能的,因为该操作符只能看到数组,而不是存储的值.

                  We want operator[] to throw if we say something like a[1] = 42, but that isn't possible because that operator only sees the index of the array, not the value being stored.

                  我们可以使用代理解决这个问题:

                  We can solve this using a proxy:

                  #include <iostream>
                  using namespace std;
                  
                  struct aproxy {
                      aproxy(int& r) : mPtr(&r) {}
                      void operator = (int n) {
                          if (n > 1 || n < 0) {
                              throw "not binary digit";
                          }
                          *mPtr = n;
                      }
                      int * mPtr;
                  };
                  
                  struct array {
                      int mArray[10];
                      aproxy operator[](int i) {
                          return aproxy(mArray[i]);
                      }
                  };
                  
                  int main() {
                      try {
                          array a;
                          a[0] = 1;   // ok
                          a[0] = 42;  // throws exception
                      }
                      catch (const char * e) {
                          cout << e << endl;
                      }
                  }
                  

                  代理类现在检查二进制数字,我们让数组的 operator[] 返回代理的一个实例,该实例对数组的内部具有有限的访问权限.

                  The proxy class now does our checking for a binary digit and we make the array's operator[] return an instance of the proxy which has limited access to the array's internals.

                  这篇关于什么是 C++ 中的代理类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:任何人都可以为我提供 C++ 中的单例示例吗? 下一篇:单例实例声明为 GetInstance 方法的静态变量,它是

                  相关文章

                  最新文章

                  <legend id='5LwfI'><style id='5LwfI'><dir id='5LwfI'><q id='5LwfI'></q></dir></style></legend>
                2. <small id='5LwfI'></small><noframes id='5LwfI'>

                  <i id='5LwfI'><tr id='5LwfI'><dt id='5LwfI'><q id='5LwfI'><span id='5LwfI'><b id='5LwfI'><form id='5LwfI'><ins id='5LwfI'></ins><ul id='5LwfI'></ul><sub id='5LwfI'></sub></form><legend id='5LwfI'></legend><bdo id='5LwfI'><pre id='5LwfI'><center id='5LwfI'></center></pre></bdo></b><th id='5LwfI'></th></span></q></dt></tr></i><div id='5LwfI'><tfoot id='5LwfI'></tfoot><dl id='5LwfI'><fieldset id='5LwfI'></fieldset></dl></div>
                        <bdo id='5LwfI'></bdo><ul id='5LwfI'></ul>

                      <tfoot id='5LwfI'></tfoot>