I have this code that compiles normally, but when I run it:
a)if I don't include the destructor, the constructor will be called twice b)If I include it the constructor will be again called twice but at the end I will get "Segmentation fault (core dumped)"
I'm new in programming generally and c++ so I can't seem to find the problem...Any help is appreciated and thanks for your time.
This is the main:
int main(){
aircraft_list<planes> plane_list;
plane_list.add();
}
This is the constructor of planes:
planes::planes(){
//initializing some vars
}
This is the code for add():
template <class T> aircraft_list<T> aircraft_list<T>::add(){
chain_node<T> *y=new chain_node<T>;
planes obj;
y->data=obj;
y->link=first;
first=y;
return *this;
}
This is the constructor for aircraft_list:
aircraft_list(){
first=0;
}
This is the destructor:
template <class T> aircraft_list<T>::~aircraft_list(){
chain_node<T> *next;
while(first){
next=first->link;
delete first;
first=next;
}
}
**EDIT:**So I changed the del() and add() to return references:
aircraft_list<T>& del(const T &x);
aircraft_list<T>& add();
and there are no problems in compiling. But after some reading I still don't understand why the constructor is called the first time.