Advertisement
Cinestra

Trying to delete an entry in an array of new pointers

Feb 5th, 2023
902
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class SNSD_member
  5. {
  6. public:
  7.     SNSD_member(string name, int position)
  8.     {
  9.         stage_name = name;
  10.         ranking = position;
  11.     }
  12.  
  13.     SNSD_member(void)
  14.     {
  15.         stage_name = "Canceled";
  16.         ranking = 0;
  17.     }
  18.  
  19.     void print()
  20.     {
  21.         cout << "Member Name: " << stage_name << endl;
  22.         cout << "Position: " << ranking << endl;
  23.     }
  24.  
  25. private:
  26.     string stage_name;
  27.     int ranking;
  28. };
  29.  
  30. int main()
  31. {
  32.     SNSD_member YoonA("YoonA", 1);
  33.     YoonA.print();
  34.  
  35.     cout << endl;
  36.  
  37.     SNSD_member* arr[3];
  38.     arr[0] = new SNSD_member("Taeyeon", 2);
  39.     arr[1] = new SNSD_member("Jessica", 3);
  40.     arr[2] = new SNSD_member("Yuri", 4);
  41.  
  42.     for (int i = 0; i < 3; i++)
  43.     {
  44.         arr[i]->print();
  45.     }
  46.  
  47.     delete arr[1];
  48.     // What happens if we try removing Jessica, can we still print out Taeyeon and Yuri?
  49.  
  50.     arr[0]->print();
  51.     arr[1]->print();
  52.     arr[2]->print();
  53.     // For whatever reason-- it can print out Taeyeon but it prints out simply the member_name and then nothing else. Yuri is not printed afterwards.
  54.  
  55.     // Maybe try to write over arr[1]?
  56.     arr[1] = arr[2];
  57.     arr[1]->print();
  58.     // Still not working...
  59.  
  60.     delete arr[0];
  61.     delete arr[2];
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement