初始化一个由 p 指向的新 INTSTK,它最多可以存储 m 个

//initialize a new INTSTK pointed by p which can store at most m ntegers

本文关键字:INTSTK 存储 一个 初始化      更新时间:2023-10-16

struct INTSTK{
int  *const e;      //points to memory allocated to store integers
const int  m;       //maximum number of integers the stack can holds
int   t;            //top indicator or number of integers in the stack;
void initSTK (INTSTK *const p, int m); 
};

我需要有关初始化STK函数定义的帮助 void initSTK(INTSTK *const p, int m( { 初始化一个由 p 指向的新 INTSTK,该 INTSTK 最多可以存储 m 个 ntegers

}

除了构造之外,您不能在其他任何地方修改const成员变量。

试试这个:

struct INTSTK{
int  *const e;      //points to memory allocated to store integers
const int  m;       //maximum number of integers the stack can holds
int   t;            //top indicator or number of integers in the stack;
INTSTK(int m_) : e(new int[m_]), m(m_), t(0) {}
};
void initSTK (INTSTK *const p, int m) {
// assuming input is already populated with type `INTSTK *const`                                                                                                                 
new (p) INTSTK(m);
}