Carlos Vargas
Carlos Vargas

Reputation: 255

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?

Upvotes: 0

Views: 75

Answers (2)

user1684306
user1684306

Reputation:

I'd like to clarify your idea of creating the auto pointer, which I hope will help.
Your goal here is to create an auto_ptr holding a DisplayController*. You could write

m_displayController = std::auto_ptr<DisplayController>( new DisplayController(x, y) );

Or have a function that returns a pointer, like this :

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

You can check out a simple example here.

Upvotes: 0

Sander De Dycker
Sander De Dycker

Reputation: 16243

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.

Upvotes: 3

Related Questions