Smart pointers and simple object management system











up vote
0
down vote

favorite












I am attempting to implement smart pointers to create a fast but relatively bulletproof object management system that allows adding, removing and retrieving objects. I come from a C# background and am trying to wrap my head around the best way to do this regarding ownership. Code:



main.cpp:



int main()
{
std::unique_ptr<scene> main_scene = std::make_unique<scene>();

std::shared_ptr<game_object> obj = std::make_shared<game_object>("obj", glm::vec3(0, 0, 0), glm::vec3(-90, 0, -90), glm::vec3(1, 1, 1));

main_scene->add_object(obj);

main_scene->remove_object(obj);

return 0;
}


scene.hpp:



namespace r3d
{
class scene
{
public:
scene::scene() { }

void add_object(std::shared_ptr<r3d::game_object>);

void remove_object(std::shared_ptr<r3d::game_object>&);

std::shared_ptr<r3d::game_object>& get_object(const char*);

private:
std::set<std::shared_ptr<r3d::game_object>> game_objects;
};
}


scene.cpp:



void scene::add_object(std::shared_ptr<game_object> ptr)
{
game_objects.insert(ptr);
}

void scene::remove_object(std::shared_ptr<game_object>& ptr)
{
game_objects.erase(game_objects.find(ptr));
ptr.reset();
}

std::shared_ptr<r3d::game_object>& scene::get_object(const char* name)
{
for(const auto& object : game_objects)
{
if(object->name == name)
{
return object;
}
}

return nullptr;
}


game_object.hpp



namespace r3d
{
class game_object
{
public:
const char* name;

game_object(const char* name, glm::vec3 position, glm::vec3 euler_angles, glm::vec3 scale) : name(name), position(position), euler_angles(euler_angles), scale(scale)
{
std::cout << "create game_objectn" << std::endl;
}

~game_object()
{
std::cout << "delete game_objectn" << std::endl;
}

protected:
glm::vec3 position;
glm::vec3 euler_angles;
glm::vec3 scale;
};
}


Calling remove_object() from main.cpp will remove both shared_ptr's and call game_object's destructor, all good, however I cannot remove the object from a different class as main.cpp will hang on to the shared_ptr.



There seems to be a few options here, I'm thinking it may be better to make obj a unique_ptr and move ownership to scene.cpp, passing back a reference to main.cpp so it can still observe the object, and have this reference for removing it.










share|improve this question




















  • 2




    Do you really want to use pointers here (even smart)? can't you deal with inplace object and references? Also, did you tried std::map or std::unordered_map (with name as a key) instead of a std::set? Can't you emplace the game_object ?
    – Calak
    yesterday

















up vote
0
down vote

favorite












I am attempting to implement smart pointers to create a fast but relatively bulletproof object management system that allows adding, removing and retrieving objects. I come from a C# background and am trying to wrap my head around the best way to do this regarding ownership. Code:



main.cpp:



int main()
{
std::unique_ptr<scene> main_scene = std::make_unique<scene>();

std::shared_ptr<game_object> obj = std::make_shared<game_object>("obj", glm::vec3(0, 0, 0), glm::vec3(-90, 0, -90), glm::vec3(1, 1, 1));

main_scene->add_object(obj);

main_scene->remove_object(obj);

return 0;
}


scene.hpp:



namespace r3d
{
class scene
{
public:
scene::scene() { }

void add_object(std::shared_ptr<r3d::game_object>);

void remove_object(std::shared_ptr<r3d::game_object>&);

std::shared_ptr<r3d::game_object>& get_object(const char*);

private:
std::set<std::shared_ptr<r3d::game_object>> game_objects;
};
}


scene.cpp:



void scene::add_object(std::shared_ptr<game_object> ptr)
{
game_objects.insert(ptr);
}

void scene::remove_object(std::shared_ptr<game_object>& ptr)
{
game_objects.erase(game_objects.find(ptr));
ptr.reset();
}

std::shared_ptr<r3d::game_object>& scene::get_object(const char* name)
{
for(const auto& object : game_objects)
{
if(object->name == name)
{
return object;
}
}

return nullptr;
}


game_object.hpp



namespace r3d
{
class game_object
{
public:
const char* name;

game_object(const char* name, glm::vec3 position, glm::vec3 euler_angles, glm::vec3 scale) : name(name), position(position), euler_angles(euler_angles), scale(scale)
{
std::cout << "create game_objectn" << std::endl;
}

~game_object()
{
std::cout << "delete game_objectn" << std::endl;
}

protected:
glm::vec3 position;
glm::vec3 euler_angles;
glm::vec3 scale;
};
}


Calling remove_object() from main.cpp will remove both shared_ptr's and call game_object's destructor, all good, however I cannot remove the object from a different class as main.cpp will hang on to the shared_ptr.



There seems to be a few options here, I'm thinking it may be better to make obj a unique_ptr and move ownership to scene.cpp, passing back a reference to main.cpp so it can still observe the object, and have this reference for removing it.










share|improve this question




















  • 2




    Do you really want to use pointers here (even smart)? can't you deal with inplace object and references? Also, did you tried std::map or std::unordered_map (with name as a key) instead of a std::set? Can't you emplace the game_object ?
    – Calak
    yesterday















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am attempting to implement smart pointers to create a fast but relatively bulletproof object management system that allows adding, removing and retrieving objects. I come from a C# background and am trying to wrap my head around the best way to do this regarding ownership. Code:



main.cpp:



int main()
{
std::unique_ptr<scene> main_scene = std::make_unique<scene>();

std::shared_ptr<game_object> obj = std::make_shared<game_object>("obj", glm::vec3(0, 0, 0), glm::vec3(-90, 0, -90), glm::vec3(1, 1, 1));

main_scene->add_object(obj);

main_scene->remove_object(obj);

return 0;
}


scene.hpp:



namespace r3d
{
class scene
{
public:
scene::scene() { }

void add_object(std::shared_ptr<r3d::game_object>);

void remove_object(std::shared_ptr<r3d::game_object>&);

std::shared_ptr<r3d::game_object>& get_object(const char*);

private:
std::set<std::shared_ptr<r3d::game_object>> game_objects;
};
}


scene.cpp:



void scene::add_object(std::shared_ptr<game_object> ptr)
{
game_objects.insert(ptr);
}

void scene::remove_object(std::shared_ptr<game_object>& ptr)
{
game_objects.erase(game_objects.find(ptr));
ptr.reset();
}

std::shared_ptr<r3d::game_object>& scene::get_object(const char* name)
{
for(const auto& object : game_objects)
{
if(object->name == name)
{
return object;
}
}

return nullptr;
}


game_object.hpp



namespace r3d
{
class game_object
{
public:
const char* name;

game_object(const char* name, glm::vec3 position, glm::vec3 euler_angles, glm::vec3 scale) : name(name), position(position), euler_angles(euler_angles), scale(scale)
{
std::cout << "create game_objectn" << std::endl;
}

~game_object()
{
std::cout << "delete game_objectn" << std::endl;
}

protected:
glm::vec3 position;
glm::vec3 euler_angles;
glm::vec3 scale;
};
}


Calling remove_object() from main.cpp will remove both shared_ptr's and call game_object's destructor, all good, however I cannot remove the object from a different class as main.cpp will hang on to the shared_ptr.



There seems to be a few options here, I'm thinking it may be better to make obj a unique_ptr and move ownership to scene.cpp, passing back a reference to main.cpp so it can still observe the object, and have this reference for removing it.










share|improve this question















I am attempting to implement smart pointers to create a fast but relatively bulletproof object management system that allows adding, removing and retrieving objects. I come from a C# background and am trying to wrap my head around the best way to do this regarding ownership. Code:



main.cpp:



int main()
{
std::unique_ptr<scene> main_scene = std::make_unique<scene>();

std::shared_ptr<game_object> obj = std::make_shared<game_object>("obj", glm::vec3(0, 0, 0), glm::vec3(-90, 0, -90), glm::vec3(1, 1, 1));

main_scene->add_object(obj);

main_scene->remove_object(obj);

return 0;
}


scene.hpp:



namespace r3d
{
class scene
{
public:
scene::scene() { }

void add_object(std::shared_ptr<r3d::game_object>);

void remove_object(std::shared_ptr<r3d::game_object>&);

std::shared_ptr<r3d::game_object>& get_object(const char*);

private:
std::set<std::shared_ptr<r3d::game_object>> game_objects;
};
}


scene.cpp:



void scene::add_object(std::shared_ptr<game_object> ptr)
{
game_objects.insert(ptr);
}

void scene::remove_object(std::shared_ptr<game_object>& ptr)
{
game_objects.erase(game_objects.find(ptr));
ptr.reset();
}

std::shared_ptr<r3d::game_object>& scene::get_object(const char* name)
{
for(const auto& object : game_objects)
{
if(object->name == name)
{
return object;
}
}

return nullptr;
}


game_object.hpp



namespace r3d
{
class game_object
{
public:
const char* name;

game_object(const char* name, glm::vec3 position, glm::vec3 euler_angles, glm::vec3 scale) : name(name), position(position), euler_angles(euler_angles), scale(scale)
{
std::cout << "create game_objectn" << std::endl;
}

~game_object()
{
std::cout << "delete game_objectn" << std::endl;
}

protected:
glm::vec3 position;
glm::vec3 euler_angles;
glm::vec3 scale;
};
}


Calling remove_object() from main.cpp will remove both shared_ptr's and call game_object's destructor, all good, however I cannot remove the object from a different class as main.cpp will hang on to the shared_ptr.



There seems to be a few options here, I'm thinking it may be better to make obj a unique_ptr and move ownership to scene.cpp, passing back a reference to main.cpp so it can still observe the object, and have this reference for removing it.







c++ pointers






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday

























asked yesterday









zooooom

484




484








  • 2




    Do you really want to use pointers here (even smart)? can't you deal with inplace object and references? Also, did you tried std::map or std::unordered_map (with name as a key) instead of a std::set? Can't you emplace the game_object ?
    – Calak
    yesterday
















  • 2




    Do you really want to use pointers here (even smart)? can't you deal with inplace object and references? Also, did you tried std::map or std::unordered_map (with name as a key) instead of a std::set? Can't you emplace the game_object ?
    – Calak
    yesterday










2




2




Do you really want to use pointers here (even smart)? can't you deal with inplace object and references? Also, did you tried std::map or std::unordered_map (with name as a key) instead of a std::set? Can't you emplace the game_object ?
– Calak
yesterday






Do you really want to use pointers here (even smart)? can't you deal with inplace object and references? Also, did you tried std::map or std::unordered_map (with name as a key) instead of a std::set? Can't you emplace the game_object ?
– Calak
yesterday

















active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209000%2fsmart-pointers-and-simple-object-management-system%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209000%2fsmart-pointers-and-simple-object-management-system%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List directoties down one level, excluding some named directories and files

list processes belonging to a network namespace

list systemd RuntimeDirectory mounts