auto_ptr vector discards qualifier

S

stephen henry

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
 
I

Ivan Vecerina

stephen henry said:
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
 
M

Michael Kurz

stephen henry said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top