template <typename T>
struct ListNode
{
struct SubNode
{
SubNode(T val,ListNode * parent) : SubNode()
{
if (debug) cout<<"SubNode constructor(T,parent)"<<endl;
this->parent=parent;
*value=val;
}
SubNode(T val) : SubNode()
{
if (debug) cout<<"SubNode constructor(T)"<<endl;
*value=val;
}
SubNode()
{
if (debug) cout<<"SubNode default constructor"<<endl;
next=parent=0;
value=new T();
}
~SubNode()
{
if (debug) cout<<"SubNode destructor"<<endl;
delete value;
}
void debug()
{
if (debug)
cout<<"\n\n-------- DEBUG PRINT -------\nstruct SubNode\nValue="<<*value<<" Value address="<<value<<"\nNext address:"<<next<<"\nParent address:"<<parent<<"\n----------------------------\n\n";
}
T * value;
SubNode * next;
ListNode * parent;
};
ListNode()
{
if (::debug) cout<<"ListNode default constructor"<<endl;
value=new T();
}
ListNode(T val) : ListNode()
{
if (::debug) cout<<"ListNode constructor(T)"<<endl;
*value=val;
}
~ListNode()
{
if (::debug) cout<<"ListNode destructor"<<endl;
delete value;
}
void debug()
{
if (debug)
cout<<"\n\n-------- DEBUG PRINT -------\nstruct ListNode\nValue="<<*value<<" Value address="<<value<<"\nNext address:"<<next<<"\nChild address:"<<child<<"\n----------------------------\n\n";
}
T * value;
ListNode * next;
SubNode * child;
};
template <typename T>
struct SpecialList
{
SpecialList()
{
if (debug) cout<<"SpecialList default constructor"<<endl;
head=new ListNode<T>();
}
SpecialList(T val)
{
if (debug) cout<<"SpecialList constructor(val)"<<endl;
head=new ListNode<T>(val);
}
~SpecialList()
{
ListNode<T> * temp;
for (ListNode<T> * current=head; head->next!=0;)
{
if (current->next!=0) temp=current->next;
cout<<"Delete element "<<current<<"\n";
delete current;
current=temp;
}
}
ListNode<T> * head;
};
Имеются ли тут какие-то ошибки?
Как создать SubNode
ListNode::SubNode <int> ошибка, не получается.
Как проверить код на утечки памяти и "не красивые" места(стат.анализ)?
Может быть уже готовые структуры для этого, скажем в stl или boost, лучше первое конечно.
Лучше value везде хранить по указателю или же по значению? Делаю по указателю чтобы при копировании, например предавая в какой либо метод, или в качестве value выступает класс какой-нибудь, чтобы реже происхдило копирование этого объекта
Спасибо за ответы