我正在寻找在我的类头文件中声明和初始化常量结构的方法。 正如您所看到的,MFC应用程序正在使用该类。 我的MFC对话框上的图层永远不会改变,所以我想不断对它们进行delcare。
我正在寻找类似的东西:
class CLayerDialog : CDialogEx
{
...
public:
const LAYER_AREA(CPoint(0, 70), CPoint(280, 140));
}
结构:
struct LAYER_AREA{
CPoint topLeft;
CPoint bottomRight;
};
最好的方法是什么,尽可能节省性能并轻松维护图层?
答案 0 :(得分:2)
您的意思是static const
成员变量吗?
// header file
class CLayerDialog : CDialogEx
{
/* ... */
public:
static const LAYER_AREA myvar;
};
// source file
const LAYER_AREA CLayerDialog::myvar(CPoint(0, 70), CPoint(280, 140));
请注意,变量必须在线外定义(在源文件中而不是头文件中)。您还需要struct LAYER_AREA
的适当构造函数。
答案 1 :(得分:1)
您可以这样做:(我对您未提供的课程做了一些假设)
头文件中的
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;
};
.cpp文件中的
CLayerDialog::CLayerDialog()
: myStructVar ( CPoint(0, 70), CPoint(280, 140) )
{
}