Created
May 18, 2021 22:28
-
-
Save GunpowderGuy/3e0424a239b4609db3d1c624451dbdba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by rudri on 10/11/2020. | |
// | |
#include <iostream> | |
#include "global.h" | |
#include "p4.h" | |
using namespace std; | |
void question_4_1(){ | |
#ifdef ENABLE_TEST | |
// puntero inteligente no inicializado | |
smart_ptr<int> sp1; | |
sp1 = make_smart_ptr<int>(10); | |
cout << *sp1 << endl; // Imprimiendo el contenido | |
// puntero inteligente inicializado | |
smart_ptr<string> sp2 = make_smart_ptr<string>("Hola"); | |
cout << *sp2 << endl; // Imprimiendo el contenido | |
#endif | |
} | |
void question_4_2(){ | |
#ifdef ENABLE_TEST | |
// puntero inteligente no inicializado | |
smart_ptr<point> sp1; | |
sp1 = make_smart_ptr<point>(20, 30); | |
cout << *sp1 << endl; // Imprimiendo el contenido | |
// puntero inteligente inicializado | |
smart_ptr<point> sp2 = make_smart_ptr<point>(10, 40); | |
cout << sp2->get_x() << endl; | |
cout << sp2->get_y() << endl; | |
#endif | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by rudri on 10/11/2020. | |
// | |
#ifndef POO2_UNIT2_TASK2_V2021_P4_H | |
#define POO2_UNIT2_TASK2_V2021_P4_H | |
#include<iostream> | |
template <typename T> | |
class smart_ptr { | |
T* interno; | |
public: | |
T& operator*() const noexcept { | |
return *interno; | |
} | |
//https://stackoverflow.com/questions/4928557/how-do-i-create-and-use-a-class-arrow-operator | |
T* operator->() const noexcept{ | |
return interno; | |
} | |
smart_ptr() = default; | |
smart_ptr(T* var) : interno(var) {} | |
~smart_ptr(){ | |
//crashea el test si es habilitado | |
//delete interno; | |
} | |
}; | |
// https://stackoverflow.com/questions/27377868/make-shared-own-implementation | |
template<typename T, typename ...Params> | |
smart_ptr<T> make_smart_ptr(Params ...params) { | |
// Completar | |
T* interno = new T(params...); //point(x,y) | |
return smart_ptr(interno); | |
} | |
class point { | |
int x; | |
int y; | |
public: | |
point(int x, int y): x{x}, y{y} {} | |
point() = default; | |
friend std::ostream& operator<< (std::ostream& os, const point& p) { | |
os << "{" << p.x <<", " << p.y << "}" << std::endl; | |
return os; | |
} | |
[[nodiscard]] int get_x() const { return x; } | |
[[nodiscard]] int get_y() const { return y; } | |
}; | |
void question_4_1(); | |
void question_4_2(); | |
#endif //POO2_UNIT2_TASK2_V2021_P4_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment