Recent Posts
printf("ho_tari\n");
SafeArray3 (template 사용) 본문
<main.cpp>
#include <iostream>
#include "safeArray.h"
#include "array.h"
int main() {
int nums1[] = {1,2,3,4,5};
SafeArray arr1(nums1,5);
for (int i = 0; i<5; i++) {
std::cout << "arr1["<<i << "] : " << arr1[i] << std::endl;
}
double nums2[] = {1.1,2.2,3.3,4.4,5.5};
SafeArray arr2(nums2,5);
for (int i = 0; i<5; i++) {
std::cout << "arr2["<<i << "] : " << arr2[i] << std::endl;
}
return 0;
}
<safeArray.h>
#ifndef SAFEARRAY_H
#define SAFEARRAY_H
#include <cassert>
#include "array.h"
template <typename T>
class SafeArray:public Array<T> {
private:
public:
explicit SafeArray(int size = Array<T>::getDefaultSize());
SafeArray(const T *pArr, int size);
virtual ~SafeArray(){}
bool operator==(const SafeArray<T>& rhs) const;
virtual T& operator[](int index);
virtual const T& operator[](int index)const;
};
template <typename T>
SafeArray<T>::SafeArray(int size)
:Array<T>(size)
{
}
template <typename T>
SafeArray<T>::SafeArray(const T *pArr, int size)
:Array<T>(pArr,size)
{
}
template <typename T>
bool SafeArray<T>::operator==(const SafeArray<T>& rhs) const {
return this->Array<T>::operator==((Array<T>)rhs);
}
template <typename T>
T& SafeArray<T>::operator[](int index) {
assert(index >= 0 && index < this->Array<T>::size_);
return this->Array<T>::operator[](index);
}
template <typename T>
const T& SafeArray<T>::operator[](int index) const{
assert(index >= 0 && index < this->Array<T>::size_);
return this->Array<T>::operator[](index);
}
#endif
<compile 결과>
'C++' 카테고리의 다른 글
SafeArray4 (try, throw, catch) (0) | 2023.09.08 |
---|---|
BoundArray2 (template 사용) (0) | 2023.09.08 |
Swap2 (template 사용) (0) | 2023.09.08 |
Queue4 (template 지정) (0) | 2023.09.08 |
Stack4 (template 지정) (0) | 2023.09.08 |