静态数据成员的初始化问题!各位高手不吝赐教!!
程序代码:
class Person { private: static char *name[10] ; .............. .............. .............. };关于这样一个指针数组静态成员怎样初始化?如果只是指针静态成员又或者是数组静态成员的初始化在网上还可以搜到解决方案,各位高手帮忙看看,万分感谢!
size_t count_calls() { static size_t ctr = 0; retrun ++ctr; } int main() { for (size_t i = 0; i != 10; ++i) { cout << count_calls() <<endl; } return 0; }静态全局变量就自己体会吧~~
#include <iostream> #include <cstdio> using namespace std; class A { public: static char *name[10]; }; char *A::name[10] = {"12345", "12345", "23456", "23456", "54321", "54321", "723", "823", "923", "1320"}; int main(void) { for(int i=0; i<10; ++i) { printf("%p\n", A::name[i]); } A::name[1] = new char[10]; printf("%p\n", A::name[1]); for (int i=0; i<9; ++i) { A::name[1][i] = '0' + i; printf("%p\n", &A::name[1][i]); } A::name[1][9] = 0; printf("%p\n", &A::name[1][9]); printf("%s\n", (char*)(0x00396f18)); cout << endl; return 0; }
#include <iostream> using namespace std; class A { public: static char *name[10]; }; char *A::name[10] ={ new char[10], new char[11], new char('3'), new char('4'), new char('5'), new char('6'), new char('7'), new char('8'), new char[30], new char('0')}; int main(void) { for (int i=0; i<10; ++i) { cout << A::name[i][0] << endl; } cout << endl; A::name[0] = "yxf"; A::name[1] = "yyy"; A::name[8] = "12345678901112131415161718190"; for (int i=0; i<10; ++i) { cout << *A::name[i] << endl; } cout << endl; for (int i=0; i<10; ++i) { cout << A::name[i] << endl; } cout << endl; return 0; }