class MyClass { int m_first; int add() }==========================\我想在程序main()中不创建对象直接使用比如x=3+MyClass::m_first; y=4+MyClass::add();-----------------------------1 是不是 m_first , add()都必须定义成静态的?2 从匈牙利表示法来看,我的变量名称有那么不合理的地方?
1, 是的 2,我喜欢这样,哈哈,至于合理不合理看楼下如何说了, .h CMyClass { static int s_nFirst; static int Add(void); }.cpp int CMyClass::s_nFirst = 0; int CMyClass::Add(void) { return 0; }
哦,居然是沙发,哈哈,果然是星期天。 static int Add(void); 加个void是c的写法,c++的写法是不写。觉得就看自己喜欢了。 static int s_nFirst; 估计通常的写法是m_nFirst;但是我觉得为了区别非静态成员变量用s_比较好,完全是个人想法。
1、是 2、函数名一般第一个字母大写,变量的话一般会加上类型,具体怎么写得看你个人习惯了 比如 int Add(); int m_iFirst; //也有人喜欢m_nFirst;
class MyClass { public: int m_first; int add() { return 8; }; } ; void main() { MyClass a; int x=7+a.add(); int y=a.m_first ; }加个pbulic 声明成公有的 就能在外部调用了
class MyClass { public: static int m_first; static int add() { return 8; }; } ; int MyClass::m_first=9;void main() { MyClass a; int x=7+MyClass::add(); int y=MyClass::m_first; }
2,我喜欢这样,哈哈,至于合理不合理看楼下如何说了,
.h
CMyClass
{
static int s_nFirst;
static int Add(void);
}.cpp
int CMyClass::s_nFirst = 0;
int CMyClass::Add(void)
{
return 0;
}
static int Add(void); 加个void是c的写法,c++的写法是不写。觉得就看自己喜欢了。
static int s_nFirst; 估计通常的写法是m_nFirst;但是我觉得为了区别非静态成员变量用s_比较好,完全是个人想法。
2、函数名一般第一个字母大写,变量的话一般会加上类型,具体怎么写得看你个人习惯了
比如 int Add();
int m_iFirst; //也有人喜欢m_nFirst;
{
public:
int m_first;
int add()
{
return 8;
};
}
;
void main()
{
MyClass a; int x=7+a.add();
int y=a.m_first ;
}加个pbulic
声明成公有的 就能在外部调用了
class MyClass
{
public:
static int m_first;
static int add()
{
return 8;
};
}
;
int MyClass::m_first=9;void main()
{
MyClass a; int x=7+MyClass::add();
int y=MyClass::m_first;
}
这句必须有