Skip to content

Instantly share code, notes, and snippets.

@utilForever
Created November 25, 2020 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save utilForever/3d4e6f8d884023aa12619ed71d1bc844 to your computer and use it in GitHub Desktop.
Save utilForever/3d4e6f8d884023aa12619ed71d1bc844 to your computer and use it in GitHub Desktop.
Simple pointer
#include<iostream>
#include<stdexcept>
template <class T>
class SimplePointer {
T* ptr;
int size;
void boundarychk(int idx);
public:
SimplePointer();
SimplePointer(int size);
SimplePointer(const SimplePointer<T>& rhs);
~SimplePointer();
int getSize();
T& operator[](int idx);
SimplePointer<T> operator+(const T& scalar);
SimplePointer<T>& operator=(const SimplePointer<T>& rhs);
template <class U> friend std::ostream& operator<< (std::ostream& os, const SimplePointer<U>& p);
};
template <class T>
SimplePointer<T>::SimplePointer(const SimplePointer<T>& rhs) {
size = rhs.size;
ptr = new T[size]; // new ptr here
std::cout << "copy constructor ptr addr: " << ptr << std::endl;
for (int i = 0; i < size; i++) {
ptr[i] = rhs.ptr[i];
}
}
template <class T>
SimplePointer<T>::SimplePointer() : SimplePointer(1) {}
template <class T>
SimplePointer<T>::SimplePointer(int size) {
ptr = new T[size]; // new ptr here
this->size = size;
std::cout << "ptr addr: " << ptr << std::endl;
for (int i = 0; i < size; i++) {
ptr[i] = 0.f;
}
}
template <class T>
SimplePointer<T>::~SimplePointer() {
delete[] ptr;
}
template <class T>
inline void SimplePointer<T>::boundarychk(int idx) {
if (idx >= size || idx < 0) {
throw std::out_of_range("index out of range");
}
}
template <class T>
inline int SimplePointer<T>::getSize() {
return size;
}
// operator[] here
template<class T>
T& SimplePointer<T>::operator[]( int idx){
this->boundarychk(idx);
return ptr[idx];
}
// operator+ here
template <class T>
SimplePointer<T> SimplePointer<T>::operator+(const T& scalar){
SimplePointer tmp(*this);
for (int i = 0; i < size; i++)
tmp.ptr[i] += scalar;
return tmp;
}
// operator= here
template <class T>
SimplePointer<T>& SimplePointer<T>::operator=(const SimplePointer<T>& rhs){
if (rhs.size != size) {
std::cout << " re-allocate ptr: " << ptr << std::endl;
delete[] this->ptr;
this->ptr = new T[rhs.size];
this->size = rhs.size;
}
for (int i = 0; i < rhs.size; i++)
this->ptr[i] = rhs.ptr[i];
return *this;
}
template <class T>
std::ostream& operator<<(std::ostream& os, const SimplePointer<T>& p) {
os << "[ ";
for (int i=0;i<p.size;i++){
os<<p.ptr[i]<< ' ';
}
os << ']';
return os;
}
using namespace std;
template <class T>
void template_test(T aval1, T aval2, T aval3){
SimplePointer<T> tet(5);
cout << "operator [] test" << endl;
cout << " before assign " << tet << endl;
tet[3] = aval1;
cout << " after assign " << tet << endl;
cout << endl;
cout << "operator + test(broadcast)" << endl;
cout << " before + " << tet << endl;
tet = tet + aval2;
cout << " after + " << tet << endl;
cout << endl;
SimplePointer<T> tet2(10);
tet2[7] = aval3;
cout << "operator = test(assign)" << endl;
cout << "tet2:" << tet2 << endl;
cout << " before = " << tet << endl;
tet = tet2;
cout << " after = " << tet << endl;
}
int main() {
cout<<" :: SimplePointer<float> test ::\n"<<endl;
template_test<float>(12.34, 5.6, 78.9);
cout<<"\n :: SimplePointer<int> test ::\n"<<endl;
template_test<int>(999, 1, 777);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment