lundi 8 octobre 2018

Making public data members unreachable in C++

I have a scenerio in which based on spec name, I want to make members of some struct unreachable. An example follows:

enum schema { IFC_1, IFC_2, IFC_3, IFC_4 };

template <typename T>
using map_type = map <schema, T>;

struct Label
{
    //Imaginary Superset of Label
    //From different specs
    struct data {
        variant <int, char, string> x;
        variant <int, char, string> y;
        variant <int, char, string> z;
        variant <int, char, string> a;
        variant <int, char, string> b;
    };

    map_type<data> abc;
    data & operator[] (const schema & key){
        data & obj = abc[key];
        /*
        if(key == IFC_1)
            then make some members unreachable.
            at compiletime or runtime
        */
        return abc[key];
    }
};

int main(){
    Label x;
    x[IFC_1].x = 0; // all member above are accesible
    x[IFC_2].x = 0; // is it possible to make x unreachable when spec is IFC_2?
    return 0;
}

In the above code, there is the Label struct and it has a std::map taking an enum class as its key and returns a struct that is defined in Label struct. Now based on the key in Label struct, I want to make arbitrary members of struct data unreachable in the main function. Is such thing possible? Can I change access permission of a struct in operator[] at runtime?

Aucun commentaire:

Enregistrer un commentaire