Search code examples
c++compilationauto-ptr

Creating an auto_ptr with 2 arguments


Hi I have a compile error when I run this code:

std::auto_ptr<MyDisplay> m_display =
    std::auto_ptr<MyDisplay>(new MyDisplay(this, m_displayController));

The error is this one:

error C2664: 'MyDisplay::MyDisplay(DemoWindow *,DisplayController*)':
   cannot convert parameter 2 from 'std::auto_ptr<_Ty>' to 'DisplayController*'

However when I pass only one argument the code is correct:

std::auto_ptr<DisplayController> m_displayController =
    std::auto_ptr<DisplayController>(US_NEW(DisplayController, this));

What is the proper way to create the pointer in the auto_ptr with 2 arguments?


Solution

  • From the error message, it appears that m_displayController is an std::auto_ptr<DisplayController>, while the MyDisplay constructor expects a DisplayController*.

    Try :

    std::auto_ptr<MyDisplay> m_display =
        std::auto_ptr<MyDisplay>(new MyDisplay(this, m_displayController.get()));
    

    or better yet, make the constructor compatible with std::auto_ptr<DisplayController>.

    As an aside : the choice of std::auto_ptr here is probably not the best. You might want to read up on the different types of smart pointers, and the different behaviors they have.