473,326 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

A stl vector of smart pointer

In Effective STL item 8, it said 'Never create cointainers of
auto_ptrs'.

But in the Boost shared_ptr_example.cpp example, it creates a stl
vector of smart pointer.
Why it is okay in this case?

Here is part the code:
struct Foo
{
Foo( int _x ) : x(_x) {}
~Foo() { std::cout << "Destructing a Foo with x=" << x << "\n"; }
int x;
/* ... */
};

typedef boost::shared_ptr<Foo> FooPtr;

struct FooPtrOps
{
bool operator()( const FooPtr & a, const FooPtr & b )
{ return a->x > b->x; }
void operator()( const FooPtr & a )
{ std::cout << a->x << "\n"; }
};

int main()
{
std::vector<FooPtr> foo_vector;
std::set<FooPtr,FooPtrOps> foo_set; // NOT multiset!

.... // omit
}

Jan 11 '06 #1
3 8190
<yi*****@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
In Effective STL item 8, it said 'Never create cointainers of
auto_ptrs'.

But in the Boost shared_ptr_example.cpp example, it creates a stl
vector of smart pointer.
'auto_ptr' is one kind of 'smart pointer'. However, not
all 'smart pointers' are 'auto_ptr's,
Why it is okay in this case?


Find out why it's *not* OK with 'auto_ptr', and
I think you'll have your answer.

-Mike
Jan 11 '06 #2
yi*****@gmail.com wrote:
In Effective STL item 8, it said 'Never create cointainers of
auto_ptrs'.

But in the Boost shared_ptr_example.cpp example, it creates a stl
vector of smart pointer.
Why it is okay in this case?
[..]


Because 'std::auto_ptr' does not satisfy the requirement imposed on
the type of the contained items.

V
Jan 11 '06 #3

yi*****@gmail.com wrote:
In Effective STL item 8, it said 'Never create cointainers of
auto_ptrs'.

But in the Boost shared_ptr_example.cpp example, it creates a stl
vector of smart pointer.
Why it is okay in this case?

Here is part the code:
struct Foo
{
Foo( int _x ) : x(_x) {}
~Foo() { std::cout << "Destructing a Foo with x=" << x << "\n"; }
int x;
/* ... */
};

typedef boost::shared_ptr<Foo> FooPtr;

struct FooPtrOps
{
bool operator()( const FooPtr & a, const FooPtr & b )
{ return a->x > b->x; }
void operator()( const FooPtr & a )
{ std::cout << a->x << "\n"; }
};

int main()
{
std::vector<FooPtr> foo_vector;
std::set<FooPtr,FooPtrOps> foo_set; // NOT multiset!

You could avoid having to create a FooPtrOps type class, by using a
smart pointer like copy_ptr:
http://code.axter.com/copy_ptr.h

The above smart pointer uses value semantics for the comparison
operators.
std::set<copy_ptr<Foo> > foo_set;

You can also use a cow_ptr
http://code.axter.com/cow_ptr.h

Both the copy_ptr and the cow_ptr can clone the pointee when needed.

Jan 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: The Directive | last post by:
I can't understand why in this folling example the message "Base.Draw()" is printed. Shouldn't "Derive.Draw" be printed because of polymorphism? How can I rewrite this example using a vector to...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
9
by: uotani.arisa | last post by:
Hi, Can someone tell me how to declare a pointer to a vector of pointers? I'm just not sure how to do this... I've tried essentially the following: vector<string *> * v; ....
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
14
by: kathy | last post by:
I have a vector which I save the pointer. function() { std::vector <CMyData*> vpMyData; vpMyData.push_back(new CMyData(0)); vpMyData.push_back(new CMyData(1)); vpMyData.push_back(new...
5
by: silverburgh.meryl | last post by:
Hi, I have a segmentation fault in line 66 of GroupResult.h and I can't figure out why that causes any problem, I appreciate if anyone can help. line 66 of Result.h: 66 size_t size()...
9
by: Someonekicked | last post by:
In my program, I need to open multiple files, and I wont know till after the program execution how many of them (user will enter that value). So I am using a vector of fstream. I am using fstream...
9
by: Timothee Groleau | last post by:
Hi all, My name is Tim, I'm just getting started with C++ and this is my first post to the group. Is there a standard recommended approach to use a vector of pointers along with <algorithm>?...
6
by: Jia | last post by:
Hi all, I have a class foo which has a static vector of pointers of type base class, and a static function to set this vector. #include <iostream> #include <vector> using namespace std;...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.