简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM