Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstring>
- class MyClass {
- char* str_;
- void Clean(){
- delete str_;
- str_ = nullptr;
- }
- static char* Copy(const char* op_str){
- char* result = nullptr;
- if(op_str != nullptr){
- size_t size = std::strlen(op_str);
- result = new char[size + 1];
- result[size] = 0;
- std::strncpy(result, op_str, size);
- }
- return result;
- }
- public:
- MyClass() : str_(nullptr) {}
- MyClass(const char* op_str) : str_(Copy(op_str)) {}
- const MyClass& operator=(const MyClass& m) {
- Clean();
- str_ = Copy(m.str_);
- return *this;
- }
- void ShowInfo() const {
- std::cout << this->GetName() << std::endl;
- }
- const char* GetName() const {
- return str_;
- }
- ~MyClass(){
- delete str_;
- }
- };
- int main() {
- int size = 3;
- MyClass* dynamicArray = new MyClass[size];
- dynamicArray[0] = MyClass("MatyeBall");
- dynamicArray[0].ShowInfo();
- delete[] dynamicArray;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement