Newsgroup: comp.lang.c++
Subject: shared_ptr and unique_ptr related question
From: somenath <somenathpal@...>
Date: Fri, 21 Feb 2014 17:28:19 -0800 (PST)
To get some understanding of shared_ptr and unique_ptr I wrote the following naive implementation of linked list.
#include<memory>
using namespace std;
template <class T>
class List {
private:
class ListItem {
public:
ListItem( T Val );
shared_ptr <ListItem > Next;
T Data;
};
shared_ptr< ListItem >Head;
public:
int CreateNode();
List() {
Head.reset();
}
void PushBack(T Val);
void Dump();
};
template<class T>
List<T>::ListItem::ListItem( T Val):Data(Val) {
Next.reset();
}
template<class T>
void List<T>::PushBack( T val)
{
unique_ptr<ListItem > NewItem (new ListItem(val));
if (!Head ) {
Head = move(NewItem);
}
else {
shared_ptr<ListItem> Curr(Head);
shared_ptr<ListItem> Prev(Head);
while( (Curr) ) {
Prev = Curr;
Curr = Curr->Next;
}
Prev->Next = move(NewItem);
}
}
template<class T>
void List<T>::Dump() {
shared_ptr<ListItem > Curr(Head);
while ( Curr) {
cout<<"Val = "<<Curr->Data<<endl;;
Curr = Curr->Next;
}
}
But I am not very clear of the correct uses of shared_ptr and auto_ptr yet. Could you please comment on the uses of smart pointers in the context of my linked list code. According to my understanding where I do not need to assign smart pointers to other one I use unique_ptr . Is this understanding correct?
Also I am not able to get convinced the benefit of shared_ptr and unique_ptr in the context of my code. I could have used even raw pointers to implement the same without losing much benefit . Is it not the case here?
I can use the above code as
int main(int argc,char *argv[] )
{
unique_ptr< List <int> > ilst (new List<int>());
ilst->PushBack(5);
ilst->PushBack(15);
ilst->PushBack(25);
ilst->Dump();
return 0;
}
Is the benefit of using smart pointers is, ilst need to be freed manually?
Please point me to some real code where smart pointers has been heavily used.
--
Somenath
Subject: shared_ptr and unique_ptr related question
From: somenath <somenathpal@...>
Date: Fri, 21 Feb 2014 17:28:19 -0800 (PST)
To get some understanding of shared_ptr and unique_ptr I wrote the following naive implementation of linked list.
#include<memory>
using namespace std;
template <class T>
class List {
private:
class ListItem {
public:
ListItem( T Val );
shared_ptr <ListItem > Next;
T Data;
};
shared_ptr< ListItem >Head;
public:
int CreateNode();
List() {
Head.reset();
}
void PushBack(T Val);
void Dump();
};
template<class T>
List<T>::ListItem::ListItem( T Val):Data(Val) {
Next.reset();
}
template<class T>
void List<T>::PushBack( T val)
{
unique_ptr<ListItem > NewItem (new ListItem(val));
if (!Head ) {
Head = move(NewItem);
}
else {
shared_ptr<ListItem> Curr(Head);
shared_ptr<ListItem> Prev(Head);
while( (Curr) ) {
Prev = Curr;
Curr = Curr->Next;
}
Prev->Next = move(NewItem);
}
}
template<class T>
void List<T>::Dump() {
shared_ptr<ListItem > Curr(Head);
while ( Curr) {
cout<<"Val = "<<Curr->Data<<endl;;
Curr = Curr->Next;
}
}
But I am not very clear of the correct uses of shared_ptr and auto_ptr yet. Could you please comment on the uses of smart pointers in the context of my linked list code. According to my understanding where I do not need to assign smart pointers to other one I use unique_ptr . Is this understanding correct?
Also I am not able to get convinced the benefit of shared_ptr and unique_ptr in the context of my code. I could have used even raw pointers to implement the same without losing much benefit . Is it not the case here?
I can use the above code as
int main(int argc,char *argv[] )
{
unique_ptr< List <int> > ilst (new List<int>());
ilst->PushBack(5);
ilst->PushBack(15);
ilst->PushBack(25);
ilst->Dump();
return 0;
}
Is the benefit of using smart pointers is, ilst need to be freed manually?
Please point me to some real code where smart pointers has been heavily used.
--
Somenath
via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/1pdpjLy
View all the progranning help forums at:
http://ift.tt/1dP9txN
No comments:
Post a Comment