7.6 Static Class Members

    • We can access a static member directly through the scope operator.

    • in general, we may not initialize astatic member inside the class. Instead, we must define and initialize each staticdata member outside the class body. 


    • Ordinarily, class static members may not be initialized in the class body. However,we can provide in-class initializers for static members that have const integral type and must do so for static members that are constexprs of literal type 

    • Even if a const static data member is initialized in the class body, that member ordinarily should be defined outside the class definition. 

      If an initializer is provided inside the class, the member’s definition must not specifyan initial value.

      class Bar { public:
      // ... private:
      static Bar mem1; // ok: static member can have incomplete type
      Bar *mem2; // ok: pointer member can have incomplete type 
      <span style="font-family: Arial, Helvetica, sans-serif;">Bar mem3; // error: data members must have complete type
      };</span>


      // for each file passed to the program
      for (auto p = argv + 1; p != argv + argc; ++p) {
      ifstream input(*p); // create input and open the file 
      <span style="font-family: Arial, Helvetica, sans-serif;">if (input)
       { // if the file is ok, ''process'' this file
      </span><span style="font-family: Arial, Helvetica, sans-serif;"><span>	</span>process(input); 
      } else
      </span><span style="font-family: Arial, Helvetica, sans-serif;">cerr << "couldn't open: " + string(*p); 
      </span><span style="font-family: Arial, Helvetica, sans-serif;">} // input goes out of scope and is destroyed on each iteration</span>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值