I wanted to make a simple linked list class and other data structures in C++ to get used to object oriented programming. After editing my old C code I had something like this (I have just included one of the functions):
template<typename T> class llist
{
public:
T data;
llist<T>* next;
llist() {next=nullptr;}
llist(const T& d) {data=d;next=nullptr;}
};
template<class T> llist<T>** llistAdd(llist<T> **l,const T& d)
{
llist<T> *temp=*l;
(*l)=new llist<T>(d);
(**l).next=temp;
return &(**l).next;
}
Which is used like:
int main()
{
llist<int>* my_integer_list = nullptr;
llistAdd(&my_integer_list,42);
llistAdd(&my_integer_list,128);
llistAdd(&my_integer_list,1337);
for(auto itr = &my_integer_list; (*itr) != nullptr; llistItrAdv(&itr))
cout<<(**itr).data<<endl;
llistClear(&my_integer_list);
return 0;
}
Which all works perfectly. The problem is that the C++ OOP-style uses methods instead of functions like llistAdd(..). The problem is that my code works with pointer-to-pointers and even pointer-to-pointer-to-pointers (see llistItrAdv(..)). If I use methods I will need to do something like:
template<typename T> llist<T>* llist<T>::Add(const T& d)
{
llist<T> *temp = new llist<T>(d);
temp->next = this;
return temp;
}
int main()
{
llist<int>* my_integer_list = nullptr;
my_integer_list = my_integer_list->Add(42);
my_integer_list = my_integer_list->Clear();
return 0;
}
This however makes ugly code and is prone to memory leaks. There must be a better way to do this with methods but I really can't think of anything. I tried to make methods for pointers, but that is illegal in C++. Could you guys educate me on how proper OOP-style deals classes like mine?
std::listorstd::vectorand let the stdlib do the hard job?