指向静态的指针
Pointer to Static
类Student
有一个静态成员Year
,该成员是指向std::string
的指针,该指针应该指向一个动态分配的数组。
class Student
{
private:
string name;
int year;
string semester;
int AmtClass;
static string *Year[4];
//Skipping the public members
};
string *Student::Year[4] = { "Freshman", "Sophomore", "Junior", "senior" };
尝试初始化Year
时出现问题:
ERROR: Cannot convert 'const char*' to 'std::string*' in initialization
为什么我得到错误?
struct A
{
static std::string *cohort;
};
std::string * A::cohort = new std::string[4];
不清楚为什么要动态分配数组。你可以用std::array<std::string, 4>
或std::dynarray<std::string>
当你更新你的帖子时,你应该写
std::string * Student::Year = new std::string[4] { "Freshman", "Sophomore", "Junior", "senior" };
或
struct Student
{
static std::string Year[];
};
std::string Student::Year[4] = { "Freshman", "Sophomore", "Junior", "senior" };
相关文章:
- 如何在类内部使用和访问静态指针到成员值s_ptm?
- C++ 返回指向函数内定义的静态数组的指针是否有效?
- decltype:使用指针访问类的静态成员
- 类的静态结构指针声明在C++
- 如何使用静态多态性在 int 和指针类型之间进行转换?
- 指向重载静态成员的函数指针 - 在unique_ptr中用作自定义删除器
- 指向模板上下文中的成员函数或静态函数的指针
- 指向 POD 类型的指针之间的静态转换与重新解释转换
- C++:如何返回指向非静态成员函数的指针?
- 获取具有静态局部变量的绑定/推断捕获 lambda 的函数指针
- C++编译时使用 constexpr 字符数组指针分配静态数组?
- 函数指针静态,构造函数
- 是否通过单元化指针UB访问静态类成员
- 静态对象指针
- 如何使用指向动态数组的静态指针创建类?
- 静态指针指向这个为mock for googletest定义的类内部
- 为静态类创建指向对象的指针是否合法?
- 指向无静态方法的指针静态方法
- 如何将原始指针静态化为指向固定数组的指针
- 显式销毁非指针静态对象