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?
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.
You can do something like this: ( I have made a few assumptions about the classes you had not provided )
in header file
class CDialogEx
{
public:
CDialogEx (){}
};
class CPoint
{
public:
CPoint ( const int& _x, const int& _y ):x(_x), y(_y){}
private:
int x;
int y;
};
struct LAYER_AREA
{
CPoint topLeft;
CPoint bottomRight;
LAYER_AREA ( CPoint tl, CPoint br ):
topLeft ( tl ), bottomRight ( br )
{
}
};
class CLayerDialog : CDialogEx
{
public:
CLayerDialog ();
const LAYER_AREA myStructVar;
};
in .cpp file
CLayerDialog::CLayerDialog()
: myStructVar ( CPoint(0, 70), CPoint(280, 140) )
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With