学习C#中……
今天发现,C#居然不支持在函数体内声明static变量,这真的是很古怪,难道一个static变量的就一定要让别人知道吗(至少会被类的其他成员知晓)?
下边的C#和C++的比较,很明显,C++在这一点上要合理很多,uniqueID这个不断递增、控制着唯一ID的重要变量,永远不会被类里的其他成员无意破坏掉,不知道为什么C#不允许这种“函数体内部的static变量“,狂汗…… class Foo { public Foo() { id_ = ++ uniqueID_; } private static int uniqueID_ = 0; private int id_; };
class Foo { public: Foo() { static int uniqueID = 0; id_ = ++ uniqueID; } private: int id_; }
|