类模板的简单应用(用于存储不同类型数据的类容器) ...

打印 上一主题 下一主题

主题 855|帖子 855|积分 2565

类模板应用

explicit

explicit 是一个关键字,用于指定该构造函数是显式构造函数。在C++中,当一个类的构造函数只有一个参数时,它可以被用于隐式类型转换,这可能会导致意想不到的行为和潜在的错误。为了避免这种情况,可以使用 explicit 关键字来声明该构造函数,表示禁止隐式类型转换,只能显式地调用该构造函数来创建对象。
  1. #include <iostream>
  2. class MyClass {
  3. public:
  4.     explicit MyClass(int x) {
  5.         std::cout << "Explicit constructor called with " << x << std::endl;
  6.     }
  7. };
  8. void func(MyClass obj) {
  9.     std::cout << "Function called" << std::endl;
  10. }
  11. int main() {
  12.     // 显式调用
  13.     MyClass obj1(10);
  14.     // 隐式调用,会触发显式构造函数,因为 MyClass 只有一个参数的构造函数,并且使用了 explicit 关键字
  15.     // MyClass obj2 = 20; // 编译错误,禁止隐式类型转换
  16.     MyClass obj3 = MyClass(20); // 显式调用
  17.     // 隐式调用,会触发隐式构造函数
  18.     func(30); // 隐式调用构造函数,然后调用函数
  19.     return 0;
  20. }
复制代码
首先实现的是构造函数,规定好必须显式的声明实例化,一个是为了隐式转换专门构造的一种方法,但实在这个过程并不是计算机进行的隐式,因为我们有专门的代码去编译。另有一个就是拷贝构造。
  1. #pragma once
  2. template<class T>
  3. class MyArray
  4. {
  5. public:
  6.     explicit MyArray(int capacity)
  7.     {
  8.         this->m_Capacity = capacity;
  9.         this->m_Size = 0;
  10.         // 如果 T 是对象,那么这个对象必须提供默认的构造函数
  11.         pAddress = new T[this->m_Capacity];
  12.     }
  13.     // 拷贝构造
  14.     MyArray(const MyArray& arr)
  15.     {
  16.         this->m_Capacity = arr.m_Capacity;
  17.         this->m_Size = arr.m_Size;
  18.         this->pAddress = new T[this->m_Capacity];
  19.         for (int i = 0; i < this->m_Size; i++)
  20.         {
  21.             this->pAddress[i] = arr.pAddress[i];
  22.         }
  23.     }
  24.     // 重载[] 操作符  arr[0]
  25.     T& operator [](int index)
  26.     {
  27.         return this->pAddress[index];
  28.     }
  29.     // 尾插法
  30.     void push_back(const T& val)
  31.     {
  32.         if (this->m_Capacity == this->m_Size)
  33.         {
  34.             return;
  35.         }
  36.         this->pAddress[this->m_Size] = val;
  37.         this->m_Size++;
  38.     }
  39.     void pop_back()
  40.     {
  41.         if (this->m_Size == 0)
  42.         {
  43.             return;
  44.         }
  45.         this->m_Size--;
  46.     }
  47.     // 获取大小的 const 成员函数
  48.     int getSize() const
  49.     {
  50.         return this->m_Size;
  51.     }
  52.     // 析构
  53.     ~MyArray()
  54.     {
  55.         if (this->pAddress != nullptr)
  56.         {
  57.             delete[] this->pAddress;
  58.             this->pAddress = nullptr;
  59.             this->m_Capacity = 0;
  60.             this->m_Size = 0;
  61.         }
  62.     }
  63. private:
  64.     T* pAddress;  // 指向一个堆空间,这个空间存储真正的数据
  65.     int m_Capacity; // 容量
  66.     int m_Size;   // 大小
  67. };
复制代码
这段代码里实现了入队和出队的功能,并且将之前学的重载操作符也用上了。测试代码里我们试图调用多个构造函数去明白代码的过程。
[code]#include #include "MyArray.hpp"using namespace std;class Person {public:        Person(){}        Person(string name, int age) {                this->name = name;                this->age = age;        }public:        string name;        int age;};void PrintArrayInt(MyArray& arr) {        for (int i = 0; i < arr.get_size(); i++) {                cout
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

冬雨财经

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表