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.
c++ pointers
add a comment |
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.
c++ pointers
2
Do you really want to use pointers here (even smart)? can't you deal with inplace object and references? Also, did you triedstd::maporstd::unordered_map(with name as a key) instead of astd::set? Can't you emplace thegame_object?
– Calak
yesterday
add a comment |
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.
c++ pointers
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
c++ pointers
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 triedstd::maporstd::unordered_map(with name as a key) instead of astd::set? Can't you emplace thegame_object?
– Calak
yesterday
add a comment |
2
Do you really want to use pointers here (even smart)? can't you deal with inplace object and references? Also, did you triedstd::maporstd::unordered_map(with name as a key) instead of astd::set? Can't you emplace thegame_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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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::maporstd::unordered_map(with name as a key) instead of astd::set? Can't you emplace thegame_object?– Calak
yesterday