nezvers

C++ Initialize array of pointers

Aug 27th, 2020 (edited)
1,920
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. /*
  2. Github: https://github.com/nezvers
  3. Youtube: https://www.youtube.com/channel/UCb4-Y0E6mmwjtawcitIAzKQ
  4. Twitter: https://twitter.com/NeZversStudio
  5. */
  6.  
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. typedef struct MyStruct //to represent some object
  11. {
  12.     int num;
  13. }MyStruct;
  14.  
  15. class MyClass{
  16. public:
  17.     MyClass()   //how to do default constructor?
  18.     {
  19.         my_list = nullptr;
  20.     }
  21.     MyClass(MyStruct *_list[], int _size)
  22.     {
  23.         my_list = _list; //How to assign it right?
  24.  
  25.         for (int i = 0; i < _size; i++)
  26.         {
  27.             cout << my_list[i]->num << endl;
  28.         }
  29.     }
  30.     ~MyClass(){}  //Warning: deleting array - those are supposed to be deleted
  31.  
  32.     MyStruct **my_list;     //How to declare it right?
  33. };
  34.  
  35. int main()
  36. {
  37.     MyStruct a,b,c,d;
  38.     a = {1};
  39.     b = {3};
  40.     c = {5};
  41.     d = {7};
  42.     MyStruct *list[] = {&a, &b, &c, &d}; //holds pointers
  43.     MyClass my_class(list, 4);
  44.     return 0;
  45. }
  46.  
Add Comment
Please, Sign In to add comment