In header file the code is:
#define ClassTP(TNm, PNm) \
class TNm; \
typedef TPt<TNm> PNm; \
class TNm{ \
private: \
TCRef CRef; \
public: \
friend class TPt<TNm>;
Why there is TNm{
there without }
following it to form a pair, is that legal? And I have no idea what's the meaning of this piece os code.
Then in other files, the Macro definition is used as follows:
ClassTP(TNotify, PNotify)//{
private:
TNotify(const TNotify&);
TNotify& operator=(const TNotify&);
public:
TNotify(){}
virtual ~TNotify(){}
virtual void OnNotify(const TNotifyType& /*Type*/, const TStr& /*MsgStr*/){}
virtual void OnStatus(const TStr& /*MsgStr*/){}
virtual void OnLn(const TStr& /*MsgStr*/){}
virtual void OnTxt(const TStr& /*MsgStr*/){}
// shortcuts for easier formationg
void OnNotifyFmt(const TNotifyType& Type, const char *FmtStr, ...);
void OnStatusFmt(const char *FmtStr, ...);
void OnLnFmt(const char *FmtStr, ...);
void OnTxtFmt(const char *FmtStr, ...);
static TStr GetTypeStr(
const TNotifyType& Type, const bool& Brief=true);
static void OnNotify(const PNotify& Notify,
const TNotifyType& Type, const TStr& MsgStr){
if (!Notify.Empty()){Notify->OnNotify(Type, MsgStr);}}
static void OnStatus(const PNotify& Notify, const TStr& MsgStr){
if (!Notify.Empty()){Notify->OnStatus(MsgStr);}}
static void OnLn(const PNotify& Notify, const TStr& MsgStr){
if (!Notify.Empty()){Notify->OnLn(MsgStr);}}
static void OnTxt(const PNotify& Notify, const TStr& MsgStr){
if (!Notify.Empty()){Notify->OnTxt(MsgStr);}}
static void DfOnNotify(const TNotifyType& Type, const TStr& MsgStr);
static const PNotify NullNotify;
static const PNotify StdNotify;
static const PNotify StdErrNotify;
};
In first line of this one, there is ClassTP(TNotify, PNotify)//{
I am curious Why the bracelet is commented out by the original programmer and no he code can still work normal.
答案 0 :(得分:1)
The C++ preprocessor performs dummy text replacement before compiling the code and expands the macro ClassTP(TNm, PNm)
into
class TNm;
typedef TPt<TNm> PNm;
class TNm{
private:
TCRef CRef;
public:
friend class TPt<TNm>;
which represents a forward declaration followed by a typedef
followed by the beginning of a class declaration. You then need to manually close the brace };
after you end the class declaration, i.e. after you're done adding member functions/variables following the macro. That's why in the example you posted there is no need for opening brace (as the macro expansion opens it), but you need to close it. The brace is commented out as otherwise the syntax is invalid. The comment is there to make you aware that that's the place the class definition begins (excluding the typedef and forward declaration).
As to what this code represents, I cannot say for sure. It looks like some kind of Observer pattern.