Skip to content

Instantly share code, notes, and snippets.

@deepanshululla
Last active August 11, 2021 04:29
Show Gist options
  • Save deepanshululla/f5ae9f77c96f8e09478ad728f0f19dd4 to your computer and use it in GitHub Desktop.
Save deepanshululla/f5ae9f77c96f8e09478ad728f0f19dd4 to your computer and use it in GitHub Desktop.
Custom string class implementation in C++
#include<iostream>
#include <cstring>
using namespace std;
template <typename T=char>
class MyString {
public:
MyString();//default constructor
explicit MyString(const T* s);
MyString(const MyString& other); // copy constructor
MyString& operator=(const MyString& other); //copy assignment operator
MyString(MyString&& other) noexcept ; // move constructor
MyString& operator=(MyString &&other) noexcept; //move assignment operator
MyString operator+(const MyString& other); // concatenation
~MyString()=default;
[[nodiscard]] const T* c_str() const;
[[nodiscard]] size_t length() const;
private:
T* d_data_p;
size_t d_size;
};
template <typename T>
std::ostream& operator<<(std::ostream& os, const MyString<T>& mystring)
{
return os << mystring.c_str();
}
template <typename T>
MyString<T>::MyString() : d_data_p(nullptr), d_size(0) {
}
template <typename T>
MyString<T>::MyString(const T* str) : d_data_p(const_cast<T *>(str)) {
if (d_data_p == nullptr) {
d_size = 0;
} else {
d_size = strlen(d_data_p);
}
}
template <typename T>
MyString<T>::MyString(const MyString &other) : d_data_p(other.d_data_p), d_size(other.d_size) {
}
template <typename T>
MyString<T> &MyString<T>::operator=(const MyString<T> &other) {
d_data_p = other.d_data_p;
d_size = other.d_size;
return *this;
}
template <typename T>
const T *MyString<T>::c_str() const {
return d_data_p;
}
template <typename T>
MyString<T> &MyString<T>::operator=(MyString<T> &&other) noexcept {
d_data_p = other.d_data_p;
d_size = other.d_size;
other.d_data_p = nullptr;
other.d_size = 0;
return *this;
}
template <typename T>
MyString<T>::MyString(MyString<T> &&other) noexcept {
d_data_p = other.d_data_p;
d_size = other.d_size;
other.d_data_p = nullptr;
other.d_size = 0;
}
template <typename T>
size_t MyString<T>::length() const {
return d_size;
}
template <typename T>
MyString<T> MyString<T>::operator+(const MyString<T> &other) {
MyString s; // create a new string named 's'
s.d_size = d_size + other.d_size;
s.d_data_p = static_cast<char *>(malloc(s.d_size + 1));
s.d_data_p = strcpy(s.d_data_p, d_data_p); // technically it should only for char, possibly find a better way?
s.d_data_p = strcat(s.d_data_p, other.d_data_p); // technically it should only for char, possibly find a better way?
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment