Implementing something like std::vector.back()
I would like to implement something like this because my application is divided into scenes and this gets sort of messy:
glEngine.scene[glEngine.current.currentScene].layer[glEngine.scene[glEngine.current.currentScene].currentLayer].Shapes.push_back(CGlShape());
instead I'd want to be able to do something like this:
glEngine.Scene().layer[glEngine.Scene().currentLayer].Shapes.push_back(CGlShape());
开发者_高级运维
How could I make a function like this?
Thanks
We have no idea what your classes are, but just make a function:
struct glEngine
{
// ...
scene_type& Scene()
{
return scene[current.currentScene];
}
};
You can also do this for Scene
, returning the current layer:
struct scene_type
{
// ...
layer_type& Layer()
{
return layer[current.currentScene];
}
};
Giving:
glEngine.Scene().Layer().Shapes.push_back(CGlShape());
You might also consider splitting the line up merely for the sake of readability:
scene_type& scene = glEngine.Scene();
layer_type& layer = scene.Layer();
layer.Shapes.push_back(CGlShape());
Lastly, the naming convention seems a bit weird, maybe rename the Scene
and Layer
functions to current_scene
and current_layer
.
Use typedef to simplify cumbersome expressions! Typedef is for that.
精彩评论