14.3 - Template classes

Templates and container classes

首先我们看下面的类

class IntArray
{
private:
    int m_nLength;
    int *m_pnData;
 
public:
    IntArray()
    {
        m_nLength = 0;
        m_pnData = 0;
    }
 
    IntArray(int nLength)
    {
        m_pnData = new int[nLength];
        m_nLength = nLength;
    }
 
    ~IntArray()
    {
        delete[] m_pnData;
    }
 
    void Erase()
    {
        delete[] m_pnData;
        // We need to make sure we set m_pnData to 0 here, otherwise it will
        // be left pointing at deallocated memory!
        m_pnData = 0;
        m_nLength = 0;
    }
 
    int& operator[](int nIndex)
    {
        assert(nIndex >= 0 && nIndex < m_nLength);
        return m_pnData[nIndex];
    }
 
    int GetLength() { return m_nLength; }
};

使用这个类,我们可以方便的创建整数数组,但是如果我们想要创建double数组,传统的做法需要创建一个全新的类。使用template class则是一个简单高效的新方法。

template <typename T>
class Array
{
private:
    int m_nLength;
    T *m_ptData;
 
public:
    Array()
    {
        m_nLength = 0;
        m_ptData = 0;
    }
 
    Array(int nLength)
    {
        m_ptData= new T[nLength];
        m_nLength = nLength;
    }
 
    ~Array()
    {
        delete[] m_ptData;
    }
 
    void Erase()
    {
        delete[] m_ptData;
        // We need to make sure we set m_pnData to 0 here, otherwise it will
        // be left pointing at deallocated memory!
        m_ptData= 0;
        m_nLength = 0;
    }
 
    T& operator[](int nIndex)
    {
        assert(nIndex >= 0 && nIndex < m_nLength);
        return m_ptData[nIndex];
    }
 
    // The length of the array is always an integer
    // It does not depend on the data type of the array
    int GetLength(); // templated GetLength() function defined below
};
 
template <typename T>
int Array<T>::GetLength() { return m_nLength; }

值得注意的是我们在类的外面定义了GetLength()成员函数。这就需要这个函数有自己的template declaration。 而且template array class是Array<T>而不是Array。

下面是个使用上述template class的例子

int main()
{
    Array<int> anArray(12);
    Array<double> adArray(12);
 
    for (int nCount = 0; nCount < 12; nCount++)
    {
        anArray[nCount] = nCount;
        adArray[nCount] = nCount + 0.5;
    }
 
    for (int nCount = 11; nCount >= 0; nCount----;)
        std::cout << anArray[nCount] << "\t" << adArray[nCount] << std::endl;
 
    return 0;
}

和template函数类似,template类也是在调用的时候由编译器实例化。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码是用 JavaScript 编写的,主要实现了创建一个用于观看视频的按钮并判断是否有未处理的普通链接。下面是每行代码的解释: 1. `function createWatchButton(url, isForPlain = false) {` 定义一个名为 `createWatchButton` 的函数,它接受两个参数 `url` 和 `isForPlain`,其中 `isForPlain` 的默认值为 `false`。 2. `let button = document.createElement("button");` 创建一个名为 `button` 的变量,它是一个 HTML 按钮元素。 3. `button.setAttribute('data-wtmzjk-mag-url', url);` 为按钮设置一个名为 `data-wtmzjk-mag-url` 的自定义数据属性,它的值为 `url` 参数。 4. `if (isForPlain) button.setAttribute('data-wtmzjk-button-for-plain', '');` 如果 `isForPlain` 参数为 `true`,则为按钮设置一个名为 `data-wtmzjk-button-for-plain` 的自定义数据属性,它的值为空字符串。 5. `button.innerHTML = <span>${T.play}</span><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><!--! Font Awesome Pro 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2022 Fonticons, Inc. --><path d="M73 39c-14.8-9.1-33.4-9.4-48.5-.9S0 62.6 0 80V432c0 17.4 9.4 33.4 24.5 41.9s33.7 8.1 48.5-.9L361 297c14.3-8.7 23-24.2 23-41s-8.7-32.2-23-41L73 39z"/></svg>;` 设置按钮的 HTML 内容,它包含一个 `<span>` 元素和一个 SVG 图标。 6. `return button;` 返回创建的按钮元素。 7. `function hasPlainMagUrlThatNotHandled() {` 定义一个名为 `hasPlainMagUrlThatNotHandled` 的函数,它没有参数。 8. `let m = document.body.textContent.match(new RegExp(reg, 'g'));` 创建一个名为 `m` 的变量,它是一个正则表达式匹配结果的数组。这里使用了一个未定义的 `reg` 变量,所以这行代码存在错误。 9. `return document.querySelectorAll([data-wtmzjk-button-for-plain]).length != (m ? m.length : 0);` 返回一个布尔值,表示是否存在未处理的普通链接。它通过查询包含自定义数据属性 `data-wtmzjk-button-for-plain` 的元素数量和匹配结果数组的长度来进行比较。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值