0

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

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
jmasterx
  • 52,639
  • 96
  • 311
  • 557

2 Answers2

3

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.

GManNickG
  • 494,350
  • 52
  • 494
  • 543
  • Wow, I cannot believe I did not know that structs could have functions!!!!!! you just saved me sooo many hours! Thanks – jmasterx Jul 24 '10 at 19:57
  • 3
    @Jex: Yup, [structs and classes are exactly the same](http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c/999810#999810), except classes default to private in everything while structs are public toe everything. – GManNickG Jul 24 '10 at 20:04
  • 1
    I know even programmin teachers of c++ that think that struct is the former C struct and they are different from classes :). – Narek Jul 26 '10 at 19:26
0

Use typedef to simplify cumbersome expressions! Typedef is for that.

Narek
  • 38,779
  • 79
  • 233
  • 389