I'm trying to do an exercise with copy assignment constructor, template and smart pointer
#include <iostream>
template <typename T>
class Stack {
private:
enum {MAX = 10};
std::unique_ptr<T[]> pitems;
int size;
int top;
public:
Stack(int n = 10){
size = n;
top = -1;
T* items = new T[n];
pitems = std::make_unique<T[]>(size);
}
Stack(const Stack & st): size(st.size), top(st.top){
//Costruttore di copia
std::unique_ptr<T[]> pitems = std::make_unique<T[]>(size);
memcpy(pitems.get(), st.pitems.get(), size);
}
~Stack(){
pitems.reset();
}
bool isempty(){
if(top == -1) return true;
return false;
};
bool isfull(){
if(top == size-1) return true;
return false;
};
// push() returns false if stack already is full, true otherwise
bool push(const T & item){
if(isfull() == false){
pitems[top] = item;
top++;
return true;
}
return false;
} // add item to stack
// pop() returns false if stack already is empty, true otherwise
bool pop(T & item){
if(isempty()) return false;
item = pitems[top-1];
top--;
return true;
}
Stack& operator=(const Stack & st){
if(this != &st){
this->size = st.size;
this->top = st.top;
this->pitems.reset(new T[this->size]);
memcpy(this->pitems.get(), st.pitems.get(), st.size);
}
}
};
int main() {
Stack<int> s = Stack<int>(2);
std::cout << s.isempty() << std::endl;
s.push(3);
int p;
std::cout << s.isfull() << std::endl;
s.pop(p);
std::cout << p << std::endl;
Stack<int> j;
j = s;
std::cout << j.pop(p) << std::endl;
return 0;
}
But when I try to copy j in s I get EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) from the compiler and I don't understand why. Futhermore I cannot understand why, when I check it with the compiler, the copy assignment operator is called twice. Sorry for the probably stupid question but I started some weeks ago to study C++.