constructor and destructor

This commit is contained in:
2026-03-15 20:57:13 -06:00
parent 2f6fa60665
commit 3edbc1aa06
2 changed files with 45 additions and 1 deletions

View File

@@ -1,8 +1,51 @@
#ifndef STACK_H
#define STACK_H
class Stack {
#include <cstdint>
#include <expected>
#define DEFAULT_STACK_SIZE 4
enum class StackErr {
ok,
bad_alloc,
empty,
};
template<typename T>
class Stack {
private:
uint64_t len;
uint64_t cap;
T *data;
public:
Stack();
~Stack();
std::expected<T, StackErr> pop();
std::expected<T, StackErr> peek();
StackErr push(T val);
void print();
};
template <typename T>
Stack<T>::Stack() {
Stack<T> new_stack;
new_stack.data = new T[DEFAULT_STACK_SIZE];
new_stack.cap = 0;
new_stack.len = 0;
return new_stack;
}
template <typename T>
Stack<T>::~Stack() {
delete[] this->data;
}
#endif // !ST

View File

@@ -1,3 +1,4 @@
#include "stack"
#include <print>
int main(void) {