I wrote a game using an array of structs to represent a 9x9 grid. Now I'm re-writing it as a class to '++ it up' a bit and to better understand classes. Part of the game used a seperate struct to keep track of selected board squares (to see if they match). Would it be proper ettiquete to include a struct as a class member, or is there a more 'proper' way?

Recommended Answers

All 8 Replies

In C++ today, the only significant difference between a struct and a class is the default access - struct, all members are public, class, they're private.

So you could rename your struct as a class, and then include it as a data member of a larger class. Or leave it as a struct if all it has are data members, no functions.

Thanks, vmanes. Sounds like I'll be making the board a class ( for the init and draw funcs ) and leave the struct for matching, just declare a struct object as a class member. Next up, re-writing again as a FSM. One thing at a time, though.

In c++ stucts can even have methods just like class. The only method I use in a struct is a constructor to initialize all the data. If you add other methods then you'd be better off just renaming the struct as class.

Well, it looks like I'm not quite getting it. Have a look, please...

class GridCell      // One cell of a 9x9 grid
{
public:
    GridCell();
    GridCell( int x, int y, int spriteNum ) // x, y = actual screen location
        :                                   // spriteNum (1-27) Match3 3x27=81 cells
    posX( x ),
    posY( y ),
    spriteNum( spriteNum ),
    Matched( false ),       
    Covered( true ),
    Selected( false ),
    Flipping( false )
    {}
    ~GridCell();
    const void SetMatched( bool mchd ) { Matched = mchd; }
    const void SetCovered( bool cvrd ) { Covered = cvrd; }
    const void SetSelected( bool slctd ) { Selected = slctd; }
    const void SetFlipping( bool flpng ) { Flipping = flpng; }
    const bool IsMatched() { return Matched; }
    const bool IsCovered() { return Covered; }
    const bool IsSelected() { return Selected; }
    const bool IsFlipping() { return Flipping; }

private:
    int posX;
    int posY;
    int spriteNum;
    bool Matched;
    bool Covered;
    bool Selected;
    bool Flipping;

};

class TileGrid : public GridCell    // This should be 81 gridcells
{
public:
    TileGrid();
    ~TileGrid()
    {
        for( int i = 0; i < BOARDSIZE; i++ )
        {
            delete Board[ i ];
        }
        delete [] Board;
    }
    GridCell GetCell( int num ) { return *Board[ num ]; }   // trying to get access
    const void Initialize();
    const void Reset();
    const void ResetMatches();
    const void SetMatch( int num );
    const void CheckMatches();
    const void Draw( D3DGraphics& gfx );
private:
    GridCell** Board;
    int posX;
    int posY;
    struct Match
    {
        int grdnm;
        int sprnm;
    } match[2];

SurfaceSequence Flips;
SurfaceSequence Tiles;
KeyedSurface CoverTile;
};

I'm jumping through hoops trying to access GridCell vars and still keep them private. Am I looking at this all wrong?

lines 16-19 and 50-54. Remove the const keyword before void.

Thanks, AD. So I don't need 'const' on funcs that set a value because something changes, right? As I've been learning, I've been encouraged to make things const if possible. Like 'const void Draw()' - I've used that before without a hitch.

you have the word const in the wrong place

void Draw() const

See this tutorial

Got it! Seems I was constipated in the brain. Now, as for all those funcs in the base class for dealing with the private data, am nI on the right track there? If so, I guess I'm on my way with it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.