Search code examples
c++structheaderconstantsdeclare

Declare and Initialize a const Struct in Class Header


I am looking for way to Declare and Initialize a constant struct in my Class Header file. The class is being used by an MFC app, as you can see. The layers on my MFC Dialog will never change, so I would like to delcare them constantly.

I am looking for something like this:

class CLayerDialog : CDialogEx
{
...
public:
   const LAYER_AREA(CPoint(0, 70), CPoint(280, 140));
}

The struct:

struct LAYER_AREA{
   CPoint topLeft;
   CPoint bottomRight;
};

What is the best way to do this, to save as much performance as possible and to easily maintain the layers?


Solution

  • Do you mean a static const member variable?

    // header file
    class CLayerDialog : CDialogEx
    {
    /* ... */
    public:
       static const LAYER_AREA myvar;
    };
    
    // source file
    const LAYER_AREA CLayerDialog::myvar(CPoint(0, 70), CPoint(280, 140));
    

    Note that the variable must be defined out-of-line (in the source file rather than the header file). You'll also need an appropriate constructor for struct LAYER_AREA as well.