pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <small id='YQf8P'></small><noframes id='YQf8P'>

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

    <tfoot id='YQf8P'></tfoot>

    • <bdo id='YQf8P'></bdo><ul id='YQf8P'></ul>
      <legend id='YQf8P'><style id='YQf8P'><dir id='YQf8P'><q id='YQf8P'></q></dir></style></legend>
      1. 可以使用模板按名稱訪問結構變量嗎?

        Can templates be used to access struct variables by name?(可以使用模板按名稱訪問結構變量嗎?)
        <tfoot id='SXuBP'></tfoot>
          <tbody id='SXuBP'></tbody>

              <i id='SXuBP'><tr id='SXuBP'><dt id='SXuBP'><q id='SXuBP'><span id='SXuBP'><b id='SXuBP'><form id='SXuBP'><ins id='SXuBP'></ins><ul id='SXuBP'></ul><sub id='SXuBP'></sub></form><legend id='SXuBP'></legend><bdo id='SXuBP'><pre id='SXuBP'><center id='SXuBP'></center></pre></bdo></b><th id='SXuBP'></th></span></q></dt></tr></i><div class="0amomkw" id='SXuBP'><tfoot id='SXuBP'></tfoot><dl id='SXuBP'><fieldset id='SXuBP'></fieldset></dl></div>
              1. <legend id='SXuBP'><style id='SXuBP'><dir id='SXuBP'><q id='SXuBP'></q></dir></style></legend>
              2. <small id='SXuBP'></small><noframes id='SXuBP'>

                  <bdo id='SXuBP'></bdo><ul id='SXuBP'></ul>
                  本文介紹了可以使用模板按名稱訪問結構變量嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  假設我有一個這樣的結構:

                  Let's suppose I have a struct like this:

                  struct my_struct
                  {
                    int a;
                    int b; 
                  }
                  

                  我有一個函數,它應該為a"或b"設置一個新值.此函數還需要指定要設置的變量.一個典型的例子是這樣的:

                  I have a function which should set a new value for either "a" or "b". This function also requires to specify which variable to set. A typical example would be like this:

                  void f(int which, my_struct* s, int new_value)
                  {
                    if(which == 0)
                       s->a = new_value;
                    else
                       s->b = new_value; 
                  }
                  

                  由于我不會在這里寫的原因,我無法將指向 a/b 的指針傳遞給 f.所以我不能用 my_struct::a 或 my_struct::b 的地址調用 f.我不能做的另一件事是在 my_struct 中聲明一個向量 (int vars[2]) 并將一個整數作為索引傳遞給 f.基本上在 f 我需要按名稱訪問變量.

                  For reasons I won't write here I cannot pass the pointer to a/b to f. So I cannot call f with address of my_struct::a or my_struct::b. Another thing I cannot do is to declare a vector (int vars[2]) within my_struct and pass an integer as index to f. Basically in f I need to access the variables by name.

                  前面例子的問題是,我打算在未來向 struct 添加更多變量,在這種情況下,我會記得向 f 添加更多 if 語句,這不利于可移植性.我可以做的一件事是將 f 寫成一個宏,如下所示:

                  Problem with previous example is that in the future I plan to add more variables to struct and in that case I shall remember to add more if statements to f, which is bad for portability. A thing I could do is write f as a macro, like this:

                  #define FUNC(which)
                  void f(my_struct* s, int new_value) 
                  { 
                          s->which = new_value; 
                  } 
                  

                  然后我可以調用 FUNC(a) 或 FUNC(b).

                  and then I could call FUNC(a) or FUNC(b).

                  這行得通,但我不喜歡使用宏.所以我的問題是:有沒有辦法使用模板而不是宏來實現相同的目標?

                  This would work but I don't like using macros. So my question is: Is there a way to achieve the same goal using templates instead of macros?

                  編輯:我將嘗試解釋為什么我不能使用指針并且我需要按名稱訪問變量.基本上,結構包含系統的狀態.該系統需要在請求時撤消"其狀態.撤消是使用名為 undo_token 的接口處理的,如下所示:

                  EDIT: I'll try to explain why I cannot use pointers and I need access to variable by name. Basically the structure contains the state of a system. This systems needs to "undo" its state when requested. Undo is handled using an interface called undo_token like this:

                  class undo_token
                  {
                  public:
                     void undo(my_struct* s) = 0;
                  };
                  

                  因此,由于多態性,我無法將指針傳遞給 undo 方法(mystruct 也包含其他類型的變量).

                  So I cannot pass pointers to the undo method because of polymorphism (mystruct contains variables of other types as well).

                  當我向結構中添加一個新變量時,我通常也會添加一個新類,如下所示:

                  When I add a new variable to the structure I generally also add a new class, like this:

                  class undo_a : public undo_token
                  {
                    int new_value;
                  public:
                    undo_a(int new_value) { this->new_value = new_value; }
                    void undo(my_struct *s) { s->a = new_value}
                  };
                  

                  問題是我在創建令牌時不知道指向 s 的指針,所以我無法在構造函數中保存指向 s::a 的指針(這可以解決問題)."b" 的類是相同的,只是我必須寫 "s->b" 而不是 s->a

                  Problem is I don't know pointer to s when I create the token, so I cannot save a pointer to s::a in the constructor (which would have solved the problem). The class for "b" is the same, just I have to write "s->b" instead of s->a

                  也許這是一個設計問題:我需要每個變量類型一個撤消令牌,而不是每個變量一個......

                  Maybe this is a design problem: I need an undo token per variable type, not one per variable...

                  推薦答案

                  #include <iostream>
                  #include <ostream>
                  #include <string>
                  
                  struct my_struct
                  {
                      int a;
                      std::string b;
                  };
                  
                  template <typename TObject, typename TMember, typename TValue>
                  void set( TObject* object, TMember member, TValue value )
                  {
                      ( *object ).*member = value;
                  }
                  
                  class undo_token {};
                  
                  template <class TValue>
                  class undo_member : public undo_token
                  {
                      TValue new_value_;
                      typedef TValue my_struct::* TMember;
                      TMember member_;
                  
                  public:
                      undo_member(TMember member, TValue new_value):
                          new_value_( new_value ),
                          member_( member )
                      {}
                  
                      void undo(my_struct *s) 
                      { 
                          set( s, member_, new_value_ );
                      }
                  };    
                  
                  int main()
                  {
                      my_struct s;
                  
                      set( &s, &my_struct::a, 2 );
                      set( &s, &my_struct::b, "hello" );
                  
                      std::cout << "s.a = " << s.a << std::endl;
                      std::cout << "s.b = " << s.b << std::endl;
                  
                      undo_member<int> um1( &my_struct::a, 4 );
                      um1.undo( &s );
                  
                      std::cout << "s.a = " << s.a << std::endl;
                  
                      undo_member<std::string> um2( &my_struct::b, "goodbye" );
                      um2.undo( &s );
                  
                      std::cout << "s.b = " << s.b << std::endl;
                  
                      return 0;
                  }
                  

                  這篇關于可以使用模板按名稱訪問結構變量嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Why do two functions have the same address?(為什么兩個函數的地址相同?)
                  Why the initializer of std::function has to be CopyConstructible?(為什么 std::function 的初始化程序必須是可復制構造的?)
                  mixing templates with polymorphism(混合模板與多態性)
                  When should I use the keyword quot;typenamequot; when using templates(我什么時候應該使用關鍵字“typename?使用模板時)
                  Dependent name resolution amp; namespace std / Standard Library(依賴名稱解析命名空間 std/標準庫)
                  gcc can compile a variadic template while clang cannot(gcc 可以編譯可變參數模板,而 clang 不能)
                      <legend id='zjGQ6'><style id='zjGQ6'><dir id='zjGQ6'><q id='zjGQ6'></q></dir></style></legend>

                      <small id='zjGQ6'></small><noframes id='zjGQ6'>

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

                            <bdo id='zjGQ6'></bdo><ul id='zjGQ6'></ul>

                              <tbody id='zjGQ6'></tbody>
                            <tfoot id='zjGQ6'></tfoot>

                          • 主站蜘蛛池模板: 无痕胶_可移胶_无痕双面胶带_可移无痕胶厂家-东莞凯峰 | 美国查特CHART MVE液氮罐_查特杜瓦瓶_制造全球品质液氮罐 | 网站建设_网站制作_SEO优化推广_百度推广开户_朋友圈网络科技 | 净化工程_无尘车间_无尘车间装修-广州科凌净化工程有限公司 | 郑州宣传片拍摄-TVC广告片拍摄-微电影短视频制作-河南优柿文化传媒有限公司 | 超声骨密度仪,双能X射线骨密度仪【起草单位】,骨密度检测仪厂家 - 品源医疗(江苏)有限公司 | 微量水分测定仪_厂家_卡尔费休微量水分测定仪-淄博库仑 | 哈尔滨京科脑康神经内科医院-哈尔滨治疗头痛医院-哈尔滨治疗癫痫康复医院 | 高精度-恒温冷水机-螺杆式冰水机-蒸发冷冷水机-北京蓝海神骏科技有限公司 | 长江船运_国内海运_内贸船运_大件海运|运输_船舶运输价格_钢材船运_内河运输_风电甲板船_游艇运输_航运货代电话_上海交航船运 | 厂厂乐-汇聚海量采购信息的B2B微营销平台-厂厂乐官网 | 工业车间焊接-整体|集中除尘设备-激光|等离子切割机配套除尘-粉尘烟尘净化治理厂家-山东美蓝环保科技有限公司 | 四川成都干燥设备_回转筒干燥机_脉冲除尘器_输送设备_热风炉_成都川工星科机电设备有限公司 | 山东螺杆空压机,烟台空压机,烟台开山空压机-烟台开山机电设备有限公司 | 精益专家 - 设备管理软件|HSE管理系统|设备管理系统|EHS安全管理系统 | TTCMS自助建站_网站建设_自助建站_免费网站_免费建站_天天向上旗下品牌 | 软瓷_柔性面砖_软瓷砖_柔性石材_MCM软瓷厂家_湖北博悦佳软瓷 | 厂房出租_厂房出售_产业园区招商_工业地产&nbsp;-&nbsp;中工招商网 | 皮带机_移动皮带机_大倾角皮带机_皮带机厂家 - 新乡市国盛机械设备有限公司 | 执业药师报名时间,报考条件,考试时间-首页入口 | 山东活动策划|济南活动公司|济南公关活动策划-济南锐嘉广告有限公司 | 24位ADC|8位MCU-芯易德科技有限公司 | 视频教程导航网_视频教程之家_视频教程大全_最新视频教程分享发布平台 | 棉柔巾代加工_洗脸巾oem_一次性毛巾_浴巾生产厂家-杭州禾壹卫品科技有限公司 | 玖容气动液压设备有限公司-气液增压缸_压力机_增压机_铆接机_增压器 | 光谱仪_积分球_分布光度计_灯具检测生产厂家_杭州松朗光电【官网】 | 神超官网_焊接圆锯片_高速钢锯片_硬质合金锯片_浙江神超锯业制造有限公司 | 好看的韩国漫画_韩漫在线免费阅读-汗汗漫画 | 高压油管,液压接头,液压附件-烟台市正诚液压附件 | 无线讲解器-导游讲解器-自助讲解器-分区讲解系统 品牌生产厂家[鹰米讲解-合肥市徽马信息科技有限公司] | 正压送风机-多叶送风口-板式排烟口-德州志诺通风设备 | SRRC认证_电磁兼容_EMC测试整改_FCC认证_SDOC认证-深圳市环测威检测技术有限公司 | 不锈钢法兰-碳钢法兰-法兰盘生产加工厂家-[鼎捷峰]-不锈钢法兰-碳钢法兰-法兰盘生产加工厂家-[鼎捷峰] | ISO9001认证咨询_iso9001企业认证代理机构_14001|18001|16949|50430认证-艾世欧认证网 | 知企服务-企业综合服务(ZiKeys.com)-品优低价、种类齐全、过程管理透明、速度快捷高效、放心服务,知企专家! | 桐城新闻网—桐城市融媒体中心主办| 丙烷/液氧/液氮气化器,丙烷/液氧/液氮汽化器-无锡舍勒能源科技有限公司 | 耐火砖厂家,异形耐火砖-山东瑞耐耐火材料厂 | 无菌水质袋-NASCO食品无菌袋-Whirl-Pak无菌采样袋-深圳市慧普德贸易有限公司 | 手持式线材张力计-套帽式风量罩-深圳市欧亚精密仪器有限公司 | TTCMS自助建站_网站建设_自助建站_免费网站_免费建站_天天向上旗下品牌 |