lundi 23 septembre 2019

How do I design an runtime-assign-valued enum?

class MyFileOperation
{
    int iUserReadPos;
    string sArticleBuffer;
    string sExtraInfo;
    enum Offset
    {
        ofsUserReadPos = 0, ofsArticleBuffer, ofsExtraInfo
    };
public:
    MyFileOperation(string filename)
    {
        /*By reading the file, I know the values of iUserReadPos, sArticlBuffer and sExtraInfo*/
        /*Then I want to assign the values Offset items*/
        ofsUserReadPos = 0;
        ofsArticlBuffer = sizeof(iUserReadPos);
        ofsExtraInfo = sizeof(string::size_type) + sArticlBuffer.length();
    }
    void MoveTo(Offset ofs)
    {
        f.seekp(ios::beg+ofs);
    }

    void SomeUsingOfMoveTo()
    {
        MoveTo(ofsExtraInfo);
    }
};

I want to assign valus for the Offset items, and directly use these values conveniently. Of course this code can't compile. I wonder if there is new c++ features such as enum class or other design can solve this problem.

Aucun commentaire:

Enregistrer un commentaire