0
#include <iostream>
using namespace std;
class Stack {
public:
    int* arr;
    int elements;
    int sizestack = 1;
public: Stack()
{
    arr = new int[sizestack];
    elements = 0;
    cout << "\nKONSTRUTOR DOMYSLNY: " << this << "\n";

}
public: Stack(int elem)
{
    sizestack = elem;
    cout << "\nKONSTRUTOR PARAMETR: " << this << "\n";
    arr = new int[sizestack];
    elements = 0;
}
public: void push(int data)
{
    if (elements >= sizestack)
    {
        return;
    }
    arr[elements] = data;
    elements++;
}
public: int top()
{
    if (elements == 0)
    {
        return 0;
    }
    return arr[elements - 1];
}
public: void pop()
{
    if (elements == 0)
    {
        return;
    }
    arr[elements] = NULL;
    elements--;
}
      bool full()
      {
          if (sizestack == elements)
          {
              return true;
          }
          return false;
      }
      bool empty()
      {
          if (elements == 0)
          {
              return true;
          }
          return false;
      }
      void destroy()
      {
          while (!empty())
          {
              pop();
          }
      }
      void print()
      {
          for (int i = 0; i < elements; i++)
          {
              cout << arr[i] << ", ";
          }
      }
public: ~Stack()
{
    cout << "\nDESTRUKTOR STOSU: " << this << "\n";
}
};
void f(Stack s, int a) {
    s.push(a);
}
int main() {
    Stack s(6);
    s.push(0);
    f(s, 1);
    f(s, 4);
    while (!s.empty()) {
        cout << "TAKI TOP: " << s.top() << "  ";
        s.pop();
    }
    return 0;
}

‎‎‎‎‎‎‎‎‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ I tried so hard and got so far, but in the end it doesn't even matter Why f(); function doesn't push element and it only prints the destructor message without the construcor one. How to fix this without changing main and f() function?

  • 2
    You have to change `f`. You need to pass the stack by reference to it. – NathanOliver Mar 29 '23 at 12:03
  • 1
    You can't fix it without changing `f`. It takes `Stack` by value (i.e. copies the object) and modifies that copy. You need to pass it by reference to have modification visible outside of `f`. – Yksisarvinen Mar 29 '23 at 12:04
  • *only prints the destructor message without the construcor one* - because you are not logging all constructors. You don't have the [Rule of Three](https://stackoverflow.com/q/4172722/7976805) implemented. – Yksisarvinen Mar 29 '23 at 12:05
  • As an FYI, you don't need to put `public:` in front of every function in C++. Just do `public:` once, then all your functions after that will be public until it sees another access specifier. – ChrisMM Mar 29 '23 at 12:21
  • *How to fix this without changing main and f() function?* You could implement your `Stack` so that it is, effectively, a singleton and any manipulation of a `Stack` through any instance affects all instances. That would may any instance a proxy for the singleton. One way to implement that is to use the monostate pattern, which has no member variables and only `static` class variables. – Eljay Mar 29 '23 at 13:04

0 Answers0