C ++ - 初始化char指针的静态数组

时间:2016-11-06 20:08:33

标签: c++ arrays pointers char initialization

如何创建指向私有静态类成员的以下指针数组?

class Auth {
private:
    static char *attribs[3];
    attribs[0]="uid";
    attribs[1]="cn";
    attribs[2]=NULL;
}

我不知道如何以及在哪里(什么在.h和什么在.cpp中?)来放置这些线......

2 个答案:

答案 0 :(得分:1)

假设您有权访问C ++ 11,您应该考虑std::stringstd::vector而不是C风格的数组和C风格的字符串

#include <vector>
#include <string>

class Auth {
    private:
        static std::vector<std::string> attribs;
};

std::vector<std::string> Auth:: attribs = {"uid", "cn"};

答案 1 :(得分:0)

auth.h

class Auth {
private:
    static char *attribs[3];
};

auth.cpp

char* Auth::attribs[3] = { "uid", "cn", NULL };