auto_ptr vector discards qualifier

Discussion in 'C++' started by stephen henry, Oct 7, 2004.

  1. 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
    stephen henry, Oct 7, 2004
    #1
    1. Advertising

  2. "stephen henry" <> wrote in message
    news:...
    > 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
    Ivan Vecerina, Oct 7, 2004
    #2
    1. Advertising

  3. stephen henry

    Michael Kurz Guest

    "stephen henry" <> schrieb im Newsbeitrag
    news:...
    >
    > 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
    Michael Kurz, Oct 7, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Siemel Naran

    auto_ptr<Derived> to auto_ptr<Base>

    Siemel Naran, Jan 10, 2005, in forum: C++
    Replies:
    2
    Views:
    1,552
    Dave Rahardja
    Jan 11, 2005
  2. danny van elsen
    Replies:
    6
    Views:
    41,229
    Pete Becker
    Apr 24, 2005
  3. Replies:
    8
    Views:
    1,915
    Csaba
    Feb 18, 2006
  4. Markus Dehmann
    Replies:
    2
    Views:
    580
    Daniel T.
    Aug 22, 2006
  5. Sousuke
    Replies:
    9
    Views:
    1,145
    Bart van Ingen Schenau
    Mar 16, 2010
Loading...

Share This Page