简体   繁体   中英

How to access struct and enum members form a class?

This seems to be a trival question with an easy solution but it has been a problem for me for some time. What I'm talking about is this piece of code:

class MainWindow
{
    public:

        enum class Difficulty
        {
            Beginner,
            Intermediate,
            Advanced
        };

        struct DifficultyLevelsProperties
        {
            struct BeginnerProperties
            {
                const int PADDING_HEIGHT = 9;
                const int PADDING_WIDTH = 9;
                const int MINES = 10;
            };

            struct IntermediateProperties
            {
                const int PADDING_HEIGHT = 16;
                const int PADDING_WIDTH = 16;
                const int MINES = 40;
            };

            struct AdvancedProperties
            {
                const int PADDING_HEIGHT = 16;
                const int PADDING_WIDTH = 40;
                const int MINES = 99;
            };
        };

        MainWindow();
        ~MainWindow();

    private:

        Difficulty difficulty = Difficulty::Beginner;
        int mines = DifficultyLevelsProperties::BeginnerProperties.MINES;
        int paddingHeight =  DifficultyLevelsProperties::BeginnerProperties.PADDING_HEIGHT;
        int paddingWidth = DifficultyLevelsProperties::BeginnerPropertiesPADDING_WIDTH;

What I am trying to achieve is pretty obvious. I tried many combinations using "." and "::" operator in different places or making them static, but still I just can't get it right. I either get "'Difficulty' is not a class or namespace" error or "expected primary-expression before '.' token". Please help. Thank you!

The problem I am looking at into your code is that you are trying to access a value that is not instantiated. I mean, when you try to assign a value to mines attribute, you are referencing the declaration, but not the value instantiated, because nothing is instantiated.

When you declare something, you are explaining how the memory is going to be structured without assign any value or behaviour.

The enumeration is working because the symbolic name is mapped into a value (old times it was mapped directly into an int type), so when you reference the symbolic value you don't get an error. This is different into mines , paddingHeight and paddingWidth . The code below is working for me:

class MainWindow
{
    public:

        enum class Difficulty
        {
            Beginner,
            Intermediate,
            Advanced
        };

        struct DifficultyLevelsProperties
        {
            struct BeginnerProperties
            {
                const int PADDING_HEIGHT = 9;
                const int PADDING_WIDTH = 9;
                const int MINES = 10;
            } beginner;

            struct IntermediateProperties
            {
                const int PADDING_HEIGHT = 16;
                const int PADDING_WIDTH = 16;
                const int MINES = 40;
            } intermediate;

            struct AdvancedProperties
            {
                const int PADDING_HEIGHT = 16;
                const int PADDING_WIDTH = 40;
                const int MINES = 99;
            } advanced;
        } levelProperties;

        MainWindow();
        ~MainWindow();

    private:

        Difficulty difficulty = Difficulty::Beginner;
        int mines = levelProperties.beginner.MINES;
        int paddingHeight = levelProperties.beginner.PADDING_HEIGHT;
        int paddingWidth = levelProperties.beginner.PADDING_WIDTH;
};

By the way, the design maybe is not the best from the point of view of SOLID principles. You are mixing handling the properties with the MainWindow. Maybe is better extract that feature from the MainWindow.

Happy coding for your minesweeper ;)

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