C++ に関する本を読んでいます。そして、自分が知っていることを少し練習する必要があると考えました。そこでクラスを作成しましたが、必要なスペースの量がわからなかったので、classname * name[]
後で割り当てるフォームのメンバーが含まれていました。new
そのため、入力しようとしてname = new classname[capacity /* a variable passed in constructor */]
もうまくいきませんでした。今考えると、これは理にかなっている。name
著書を参照したところ、それは と同じであることに気づきました&name[0]
。これは、私のIDEが「式は変更可能な左辺値でなければならない」と言った理由を説明しています。だから今、私の質問は、ある行で配列を宣言し、new
別の行でそれを割り当てるにはどうすればよいですか? type * name[]
クラスメンバーとしては有効であるのに、クラス外では有効ではない理由も知りたいですか?
class MenuItem
{
public:
MenuItem(string description):itsDescription(description) {};
void setDescription(string newDescription);
string getDescription() const;
private:
string itsDescription;
};
void MenuItem::setDescription(string newDescription)
{
itsDescription = newDescription;
}
string MenuItem::getDescription() const
{
return itsDescription;
}
class Menu
{
public:
Menu(int capacity);
private:
MenuItem * items[];
};
Menu::Menu(int capacity)
{
items = new MenuItem("")[capacity];
}
どんな助けでも大歓迎です。