Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   auto_ptr vector discards qualifier (http://www.velocityreviews.com/forums/t286088-auto_ptr-vector-discards-qualifier.html)

stephen henry 10-07-2004 11:30 AM

auto_ptr vector discards qualifier
 
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

Ivan Vecerina 10-07-2004 11:49 AM

Re: auto_ptr vector discards qualifier
 
"stephen henry" <stehenry@yahoo.com> wrote in message
news:845db307.0410070330.1f066d9f@posting.google.c 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



Michael Kurz 10-07-2004 12:27 PM

Re: auto_ptr vector discards qualifier
 

"stephen henry" <stehenry@yahoo.com> schrieb im Newsbeitrag
news:845db307.0410070330.1f066d9f@posting.google.c 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




All times are GMT. The time now is 12:14 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.