0

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?

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
Carlos Vargas
  • 254
  • 6
  • 18
  • What's `MyDisplay`? And as @sharth says, why are you using `auto_ptr` because unless you have a *very* good reason, don't! – Paul Evans Jul 23 '14 at 15:31
  • 2
    Please don't use [`std::auto_ptr`](http://en.cppreference.com/w/cpp/memory/auto_ptr), it's been __deprecated__. You're going to cause yourself no end of heartache. Please use [`std::shared_ptr`](http://en.cppreference.com/w/cpp/memory/shared_ptr) or [`std::unique_ptr`](http://en.cppreference.com/w/cpp/memory/unique_ptr) instead. – Bill Lynch Jul 23 '14 at 15:34
  • 1
    This code is why `std::auto_ptr` is terrible: https://gist.github.com/sharth/637dfafbf579183f5d7b – Bill Lynch Jul 23 '14 at 15:40

2 Answers2

3

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.

Sander De Dycker
  • 16,053
  • 1
  • 35
  • 40
  • 3
    And then he's passed a `std::auto_ptr` by value, which means that the original `mDisplayController` then points to null... Which is why `std::auto_ptr` is terrible... – Bill Lynch Jul 23 '14 at 15:33
  • How come I didn't see that I needed a DisplayController*, pretty obvious. Thanks it did the trick. I don't get why the displayController would be null after? I am passing a pointer right? – Carlos Vargas Jul 23 '14 at 16:03
  • @sharth Unless, of course, that's what he wants. (I've used `auto_ptr` in the interface of an inter-thread queue. It more or less guarantees that only one thread at a time has access to the object.) – James Kanze Jul 23 '14 at 16:08
0

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.