Edit: add .h file.
#pragma once
#include "Screen.h"
#include "../IniParams.h"
class AtcParamsMenu : public Screen
{
public:
AtcParamsMenu(ScreenManager *screenMgr, MenuTypes MenuType, IniParams *Params);
~AtcParamsMenu();
private:
enum eFields
{
make,
model,
connector
};
void processKey();
void updateScreen(bool refreshAll);
void selectMaker();
void deSelectMaker();
void setMaker(AtcManufacturer maker);
void setModel();
void setCabinetType();
unsigned char idx;
unsigned char fieldIdx;
const unsigned char addY;// = 1
const unsigned char xIdx;// = 14
const unsigned char dispXidx; // = 15
const unsigned char fieldLen; // = 9
static const eFields minField = make;
static const eFields maxField = connector;
IniParams *params;
MenuTypes menuType;
};
Here is a class constructor.
Note the last 4 variables are constant unsigned chars from the .h file. This is not a base class, but I prefer not to use static for these.
Is this considered a 'clean' way to initialize constants? Some of my classes might end up with 7 or 8 of these.
AtcParamsMenu::AtcParamsMenu(ScreenManager *screenMgr, MenuTypes MenuType, IniParams *Params) :
Screen(screenMgr), addY(1), xIdx(14), dispXidx(15), fieldLen(9)
We can't see enough of your code to give a full review of the situation, but here are some notes:
For simple initialisers like this, since C++11 you can provide them inline:
struct Foo { ScreenManager* screenMgr; const int myConstant = 42; Foo(ScreenManager* screenMgr); }; Foo::Foo(ScreenManager* screenMgr) : screenMgr(screenMgr) {}
However, since the initialisations don't depend on any inputs at all, and don't appear to be instance-specific, they should probably be static const
, despite your reservations
If they are static const
, you do not initialise them in the constructor, but define them separately. Like this:
struct Foo { static const int myConstant; ScreenManager* screenMgr; Foo(ScreenManager* screenMgr); }; const int Foo::myConstant = 42; Foo::Foo(ScreenManager* screenMgr) : screenMgr(screenMgr) {}
In c++11 you can initialize directly in the class declaration which looks more cleaner :
class AtcParamsMenu
{
const int addY = 1;
const int xIdx = 14;
};
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.