0

I am writing a new application and want to start getting into C++ 11.

I have 2 questions:

Q1:

Should i not be using * nowadays?

I can see that the unique_ptr<Type> varName( new Type() ) seems to be preferred?

Q2:

If the above is true, is something like this correct:

h:

class App : public BaseClass
{
    // Methods
public:

    App();
    ~App();

private:

    // Properties
public:

    unique_ptr<WindowInfo> GetMainWindowInfo() const;
    void SetMainWindowInfo( unique_ptr<WindowInfo> windowInfo );

private:

    unique_ptr<WindowInfo> mainWindowInfo;

};

cpp:

App::App()
{
    // Set up the main app window
    unique_ptr<WindowInfo> windowInfo( new WindowInfo() );

    windowInfo->SetTitle( L"Blah" );
    windowInfo->SetHeight( CW_USEDEFAULT );
    windowInfo->SetWidth( CW_USEDEFAULT );

    this->SetMainWindowInfo( windowInfo );
}

App::~App()
{  
    // Probs don't need this anymore right?
    delete this->mainWindowInfo;
    this->mainWindowInfo = nullptr;
}

unique_ptr<WindowInfo> App::GetMainWindowInfo() const
{
    return this->mainWindowInfo;
}

void App::SetMainWindowInfo( unique_ptr<WindowInfo> windowInfo )
{
    this->mainWindowInfo = windowInfo;
}

The user of App is that it will be a way of getting all that information about my main window, thus it could be used throughout my application...

Edit::

After reading the herbsutter article, i am a little confused at an error i'm getting:

Error 7 error C2280: 'std::unique_ptr>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' : attempting to reference a deleted function c:\users\james\documents\visual studio 2013\projects\platformgameengine\platformgameengine\app.cpp 21 1 PlatformGameEngine

The code i'm using is the code above.

Edit 2: RE MikeMB

Should i be doing this then?:

WindowInfo* App::GetMainWindowInfo() const
{
    return this->mainWindowInfo.get;
}
Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233
  • 2
    Please read the following: http://herbsutter.com/2013/06/05/gotw-91-solution-smart-pointer-parameters/ – Brian Bi May 01 '15 at 00:34
  • Ah okay, so considering you have to use them so much, is there a faster way of typing them?.. Or are we lumbered with the longer way of writing them? P.S @Brian - That's uber helpful! – Jimmyt1988 May 01 '15 at 00:36
  • 1
    You *can* use macros, but that doesn't mean you *should*. – The name's Bob. MS Bob. May 01 '15 at 00:40
  • 1
    @Jimmyt1988 you should try using smart pointers whenever possible. `unique_ptr`s have zero overhead w.r.t. raw pointers, however their usage is limited to unique ownership. If you want shared ownership, you can use `std::shared_ptr`. The nice thing is that deallocation is then done when the pointers is going out of scope or at destruction of an object that contains the smart pointer, so there's no more need for `delete`. You can also use a `typedef` or `using` directive to simplify the declarations: `using my_uptr = std::unique_ptr>;` – vsoftco May 01 '15 at 00:44
  • I'm a little confused whether the parameters, for example: `SetMainWindowInfo` function, should use `WindowInfo* windowInfo` as? The herbsutter is quite detailed and thus confusing for me. – Jimmyt1988 May 01 '15 at 00:48
  • There are A LOT of cases, where pointers don't imply any ownership. E.g. your `GetMainWindowInfo()` isn't supposed to transfert ownership to the caller, so there is no point in using a smart pointer as a return value (not to mention the fact that your compiler should complain anyway. – MikeMB May 01 '15 at 01:01
  • 1
    @dr_andonuts: Why would you use macros? Just write an aliase: `template using uptr=std::unique_ptr` – MikeMB May 01 '15 at 01:03
  • Voted to reopen the question, as especially the second part and the edit are not covered by the referenced question (and only partially by the answer) – MikeMB May 01 '15 at 01:07
  • 1
    Replying to your edit: You probably want to return a pointer to const (`const WindowInfo *`) and you missed the parentheses after `get` , but essentially yes. – MikeMB May 01 '15 at 01:13

0 Answers0