How would i write the following with smart ptrs:
class App
{
public:
Project* GetProject();
SetProject( Project* project );
private:
Project* project;
}
Project* project = new Project();
App* app = new App();
app.SetProject( project );
Project* retrievedProject = app.GetProject();
I am mostly interested in the method paramters and return techniques... Is something like this correct?
class App
{
public:
void App::SetProject( unique_ptr<Project> project )
{
this->project.swap( project );
}
Project* App::GetProject() const
{
return this->project.get();
}
private:
unique_ptr< Project > project;
}
...
unique_ptr< Project > project( new Project() );
unique_ptr< App > app( new App() );
app.SetProject( project );
unique_ptr< Project > retrievedProject = app.GetProject();
This is related to this question: Should i stop using * pointers? - A few entry questions
I am slightly confused at what the rules are now... Thanks in advance!
Edit for minor question:
Galik, you had mentioned that it's rare that you want to declare Project outside of the function call and then pass it into with release... I have the following (for example):
unique_ptr<Project>project( new Project() );
project.Title = "Bob";
project.Type = PROJECTYPE.DISTRIBUTION;
project.Manager = manager;
app.SetProject( project.release() );
Is this perfectly normal? My issue is that I am actually creating tools for gaming and i'm worried that the constant flagging for deletion is going to be causing micro stutters as garbage collection is kicking in... Am I just worrying for worry sake?