Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > auto_ptr vector discards qualifier

Reply
Thread Tools

auto_ptr vector discards qualifier

 
 
stephen henry
Guest
Posts: n/a
 
      10-07-2004
Hi all,

I'm trying to create a vector of pointers to a class:

For example:

class CMb ...

std::vector< CMb* > myvect(100);

myvect[10] = new CMb();

But this doesn't work. g++ just spews out a whole load of uninituitive
errors around the assignment operation.

So, have just read a post on this newsgroup from a few years ago, I'm
trying to use an auto_ptr instead.

so,

std::vector< std::auto_ptr<CMb> > myvect(100);
myvect[10] = new CMb();

But, this time the compiler complains that passing const
std::auto_ptr<CMb> discards a qualifier.

How to I fix this?

Thanks,

Stephen
 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      10-07-2004
"stephen henry" <> wrote in message
news: om...
> I'm trying to create a vector of pointers to a class:
>
> For example:
>
> class CMb ...
>
> std::vector< CMb* > myvect(100);
>
> myvect[10] = new CMb();
>
> But this doesn't work. g++ just spews out a whole load of uninituitive
> errors around the assignment operation.


The following code sample compiles without problem:
#include <vector>
class CMb {};

int main()
{
std::vector< CMb* > myvect(100);
myvect[10] = new CMb;
}

What was the error you were encountering?

> So, have just read a post on this newsgroup from a few years ago, I'm
> trying to use an auto_ptr instead.
>
> so,
>
> std::vector< std::auto_ptr<CMb> > myvect(100);
> myvect[10] = new CMb();
>
> But, this time the compiler complains that passing const
> std::auto_ptr<CMb> discards a qualifier.


It is illegal to use a container of std::auto_ptr
(because instances of auto_ptr cannot properly be copied).
You could use another smart pointer instead, however,
such as boost::shared_ptr (from www.boost.org), which
will be adopted in the next revision of the standard.


hth,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form


 
Reply With Quote
 
 
 
 
Michael Kurz
Guest
Posts: n/a
 
      10-07-2004

"stephen henry" <> schrieb im Newsbeitrag
news: om...
>
> class CMb ...
>
> std::vector< CMb* > myvect(100);
>
> myvect[10] = new CMb();


with a dummy class "class CMb {};" the above code compiles on my gcc 3.3.4.
and so does my VC7 13.10.3052.

>
>
> std::vector< std::auto_ptr<CMb> > myvect(100);
> myvect[10] = new CMb();
>


I can hardly believe that using std::auto_ptr with a std::vector is safe as
std::auto_ptr does not have
value semantics as recommended for std::vector.
(The implementation of std::vector could use temporary copies, which would
discard all your pointers)



Best Regards
Michael


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
explicit auto_ptr<T>::auto_ptr(T*) ? Sousuke C++ 9 03-16-2010 11:54 AM
make_pair with auto_ptr: ... discards qualifiers Markus Dehmann C++ 2 08-22-2006 04:34 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
auto_ptr<Derived> to auto_ptr<Base> Siemel Naran C++ 2 01-11-2005 04:45 AM
Why can't push a "const auto_ptr" into a "vector" bucher C++ 1 10-22-2003 03:36 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57