我有这样的事情:
struct cd { char name; cd *next;}
// some code...
int main(){
char title[100];
// some code...
cd *p =new cd;
p->name=title;
Run Code Online (Sandbox Code Playgroud)
如何将阵列复制title到p->name?
如果你使用a std::string,这很容易:
struct cd { std::string name; cd *next; };
int main() {
// blah
p->name = title;
}
Run Code Online (Sandbox Code Playgroud)
但你可以比这更好.在C++中,您可以使用构造函数初始化对象:
struct cd {
cd(std::string newname) : name(newname), next() {}
std::string name;
cd *next;
};
int main() {
// blah
cd p(title); // initializes a new cd with title as the name
}
Run Code Online (Sandbox Code Playgroud)
如果构造函数不合需要,可以使用聚合初始化:
struct cd {
std::string name;
cd *next;
};
int main() {
// blah
cd p = { title, NULL }; // initializes a new cd with title as the name
// and next as a null pointer
}
Run Code Online (Sandbox Code Playgroud)