看到宏声明的一段代码,不知其意求解~
大家好,我是一个C++初学者,最近在看一本书《MUD游戏编程》,第190页上有段宏声明,看不懂声明意思,我摘录了其中的代码,求大家帮忙分析一下:
DatabasePointer.h
- C/C++ code
#ifndef SIMPLEMUDDATABASEPOINTER_H#define SIMPLEMUDDATABASEPOINTER_H#include <iostream>using std::ostream;using std::istream;// ======================================================// This is the DATABASEPOINTER macro, which defines a // database pointer proxy class. Why macros? Because// I've learned that templates + circular dependencies// are a VERY BAD combination when dealing with simple// one pass compilers, like C++.// ======================================================#define DATABASEPOINTER( pt, t ) \class t; \class pt \{ \public: \ pt( int p_id = 0 ) \ : m_id( p_id ) {} \ \ pt& operator=( int p_id ) \ { \ m_id = p_id; \ return *this; \ } \ \ operator int() \ { \ return m_id; \ } \ \ t& operator*(); \ t* operator->(); \ operator t*(); \ \ int m_id; \}; \ \inline ostream& operator<<( ostream& s, const pt& p ) \{ \ s << p.m_id; \ return s; \} \ \inline istream& operator>>( istream& s, pt& p ) \{ \ s >> p.m_id; \ return s; \}namespace SimpleMUD{DATABASEPOINTER( player, Player )DATABASEPOINTER( item, Item )} // end namespace SimpleMUD#endif
[解决办法]
\是连接符号,
DATABASEPOINTER( pt, t ) 的意思就是实现了一个名字是pt的类,和申明一个类t.他们2有代码里的关系..
这样写不大好,不好调试.
[解决办法]
[解决办法]
是重载。。。。两次也不怕啊,你原本就有这个运算符对不对?那么你第一次重载不就重复了?(呵呵按照你的想法),所以,不会重复的
[解决办法]
我第一反应是为什么不用模板, 看了注释居然说VERY BAD,谁解释一下呗。。
[解决办法]
编译选项加/EP /P,重新编译,查看宏展开后对应的.i文件。
[解决办法]
只是声明了一下,这几个运算符,。。。也是重载