类模板应用
explicit
explicit 是一个关键字,用于指定该构造函数是显式构造函数。在C++中,当一个类的构造函数只有一个参数时,它可以被用于隐式类型转换,这可能会导致意想不到的行为和潜在的错误。为了避免这种情况,可以使用 explicit 关键字来声明该构造函数,表示禁止隐式类型转换,只能显式地调用该构造函数来创建对象。- #include <iostream>
- class MyClass {
- public:
- explicit MyClass(int x) {
- std::cout << "Explicit constructor called with " << x << std::endl;
- }
- };
- void func(MyClass obj) {
- std::cout << "Function called" << std::endl;
- }
- int main() {
- // 显式调用
- MyClass obj1(10);
- // 隐式调用,会触发显式构造函数,因为 MyClass 只有一个参数的构造函数,并且使用了 explicit 关键字
- // MyClass obj2 = 20; // 编译错误,禁止隐式类型转换
- MyClass obj3 = MyClass(20); // 显式调用
- // 隐式调用,会触发隐式构造函数
- func(30); // 隐式调用构造函数,然后调用函数
- return 0;
- }
复制代码 首先实现的是构造函数,规定好必须显式的声明实例化,一个是为了隐式转换专门构造的一种方法,但实在这个过程并不是计算机进行的隐式,因为我们有专门的代码去编译。另有一个就是拷贝构造。- #pragma once
- template<class T>
- class MyArray
- {
- public:
- explicit MyArray(int capacity)
- {
- this->m_Capacity = capacity;
- this->m_Size = 0;
- // 如果 T 是对象,那么这个对象必须提供默认的构造函数
- pAddress = new T[this->m_Capacity];
- }
- // 拷贝构造
- MyArray(const MyArray& arr)
- {
- this->m_Capacity = arr.m_Capacity;
- this->m_Size = arr.m_Size;
- this->pAddress = new T[this->m_Capacity];
- for (int i = 0; i < this->m_Size; i++)
- {
- this->pAddress[i] = arr.pAddress[i];
- }
- }
- // 重载[] 操作符 arr[0]
- T& operator [](int index)
- {
- return this->pAddress[index];
- }
- // 尾插法
- void push_back(const T& val)
- {
- if (this->m_Capacity == this->m_Size)
- {
- return;
- }
- this->pAddress[this->m_Size] = val;
- this->m_Size++;
- }
- void pop_back()
- {
- if (this->m_Size == 0)
- {
- return;
- }
- this->m_Size--;
- }
- // 获取大小的 const 成员函数
- int getSize() const
- {
- return this->m_Size;
- }
- // 析构
- ~MyArray()
- {
- if (this->pAddress != nullptr)
- {
- delete[] this->pAddress;
- this->pAddress = nullptr;
- this->m_Capacity = 0;
- this->m_Size = 0;
- }
- }
- private:
- T* pAddress; // 指向一个堆空间,这个空间存储真正的数据
- int m_Capacity; // 容量
- int m_Size; // 大小
- };
复制代码 这段代码里实现了入队和出队的功能,并且将之前学的重载操作符也用上了。测试代码里我们试图调用多个构造函数去明白代码的过程。
[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 |