相关疑难解决方法(0)

是否有可能在我的模板化类中有一个静态成员变量,而该类的用户不必知道它?

我有一个模板化的容器类,类似于这个玩具代码:

template <class ItemType> class MyVector
{
public:
   MyVector() : _numItems(0), _items(NULL) {/* empty */}

   /** Returns a reference to the first item in our array,
     * or a default-constructed item if the array is empty.
     */
   const ItemType & GetFirstItemWithDefault() const
   {
      return (_numItems > 0) ? _items[0] : _defaultItem;
   }

   [other methods omitted because they aren't relevant]

private:
   int _numItems;       // how many valid items (_items) points to
   ItemType * _items;   // demand-allocated
   const ItemType _defaultItem;
};
Run Code Online (Sandbox Code Playgroud)

这个类非常方便使用 …

c++ static templates member-variables

7
推荐指数
2
解决办法
192
查看次数

标签 统计

c++ ×1

member-variables ×1

static ×1

templates ×1