fcamuso

Corso C++ 41 (explicit, copy constructor e =delete)

Jul 16th, 2026
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. import std;
  2.  
  3. class PuntoQ1 {
  4.  
  5.    public:
  6.       PuntoQ1() = default; //per creare il punto (0,0)
  7.  
  8.       //costruttore 'standard' (configurazione parametri completa)
  9.       //definito anche costruttore 'target' per le deleghe
  10.       PuntoQ1(int x, int y):
  11.         x_{x >= 0 ? x : throw std::invalid_argument("x negativo")},
  12.         y_{y >= 0 ? y : throw std::invalid_argument("y negativo")}
  13.       {   }
  14.  
  15.       //costruttore che delega al precedente
  16.       //per creare un punto con coordinate uguali (sulla bisettrice)
  17.       explicit PuntoQ1(int x) : PuntoQ1(x,x) {}
  18.  
  19.       //PuntoQ1(double x) : x_{x}, PuntoQ1(0,0) {} NO: o delega o inizializzazione
  20.  
  21.      
  22.      //costruttore che accetta stringhe
  23.      //esempio PuntoQ1( "(3, 6)" )
  24.      //(delegante)
  25.      PuntoQ1(const std::string& s) : PuntoQ1(
  26.           std::stoi(s.substr(1, s.find(',') - 1)),
  27.           std::stoi(s.substr(s.find(',') + 1))   //PuntoQ1(3,6)
  28.       ) {}
  29.  
  30.      //tag
  31.      struct Polare {};
  32.  
  33.      PuntoQ1(int raggio, int angolo_radianti, Polare p)
  34.          : PuntoQ1(raggio* std::cos(angolo_radianti),
  35.                    raggio* std::sin(angolo_radianti)) {}
  36.  
  37.  
  38.  
  39.  
  40.   //metodi getter / setter
  41.   [[nodiscard]] auto x() const -> int {return x_;}
  42.   [[nodiscard]] auto y() const -> int {return y_;}
  43.  
  44.   auto set_x(int x) -> void {
  45.  
  46.     if (x<0)
  47.         throw std::invalid_argument("x negativo");
  48.    
  49.     x_ = x;
  50.   }
  51.  
  52.     private:  
  53.       int x_{};
  54.       int y_{};
  55.    
  56. };
  57.  
  58. void vecchia_funzione(PuntoQ1 p) {
  59.  
  60.    //PuntoQ1 {n, 2*n};
  61.    //ecc.
  62. }
  63.  
  64. class NPC {
  65.    
  66.  private:
  67.     inline static int id_counter = 0;
  68.  
  69.     int id_{}; //DEVE essere unico per ciascun npc            
  70.  
  71. public:
  72.     // Costruttore normale
  73.     explicit NPC()
  74.         : id_(++id_counter) {}
  75.  
  76.     auto [[nodiscard]] id() const -> int { return id_; }
  77.  
  78.     //costruttore copia (copy constructor)
  79.     //NPC(const NPC& npc_da_copiare): NPC() {
  80.     //  
  81.     //}
  82.  
  83.     NPC(const NPC& npc_da_copiare) = delete;
  84.  
  85. };
  86.  
  87. auto fnpc(NPC n) -> void {}
  88.  
  89.  
  90. auto main() -> int
  91. {
  92.     auto p1 = PuntoQ1{10, 34}; //OK
  93.     std::println("{}", p1.x());
  94.  
  95.     std::string letta_da_file = "(12, 1)";
  96.     auto p2 = PuntoQ1(letta_da_file);
  97.  
  98.     std::println("{} {}", p2.x(), p2.y());
  99.  
  100.     auto p3 = PuntoQ1(12, 1, PuntoQ1::Polare{});
  101.     std::println("{} {}", p3.x(), p3.y());
  102.  
  103.     //chiamata con vecchia firma
  104.     //vecchia_funzione(5); NO: explicit
  105.  
  106.     // Frazione f = 5;
  107.     auto p4 = PuntoQ1({3,4});
  108.    
  109.     auto npc1 = NPC{};
  110.     auto npc2= NPC{};
  111.     //auto npc2 = npc1; NO =delete
  112.     //fnpc(npc1); NO =delete
  113.  
  114.     std::println("npc1 id: {} npc2 id {}", npc1.id(), npc2.id());
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment