#include <iostream>
using namespace std;
class MyClass
{
public:
MyClass(){ mchar = 'A';};
static char *str ;
static void print( void );
friend void printprivate( MyClass &myclass );
private:
char mchar;
};
char * MyClass:: str = "This is a static var";
void MyClass::print( void )
{
cout << str << endl;
}
void printprivate( MyClass &myclass )
{
cout << myclass.mchar <<endl;
}
void main()
{
MyClass::print();
MyClass myclass;
printprivate( myclass );
}
char * MyClass:: str = "This is a static var";在初始化 str的时候,因改讲类型写完整
例如:
你声明的是:
static char *str;
那么你在初始化的时候就该这样初始化:
char * MyClass::str = “this is a static var”;
friend :有元函数,不是类的一部分,但是却能访问类中的私有成员.