我一直在学习 C++ 中的课程,我从一本旧的俄罗斯书籍中得到了一些关于书籍类的代码,我尝试修改它并运行它它不起作用可能有助于我理解为什么作者使用这个代码(strdup 是什么做?)
Author = strdup(autho);
在构造函数内部并且这行代码有误
Book s("edgar", "science", "chemistry for dummies", "502","12.11.13","1.12.96");
有人有简单直接的解释吗?
主要代码如下
using namespace std;
class Book{
char * Author;
char * Type;
char * Title;
int * Pages;
unsigned int * Yearpublished;
unsigned int * Publishing;
Book(char * autho, char * type, char * title, int * pages, unsigned int * yearpublished, unsigned int * publishing ){
Author = strdup(autho);
Type = strdup(type);
Title = strdup(title);
Pages = pages;
Yearpublished = yearpublished;
Publishing = publishing;
}
~Book(){
if(Author != NULL){
free(Author);
}
if(Type != NULL){
free(Type);
}
if(Title != NULL){
free(Title);
}
}
};
int main(){
cout << "main start" << endl;
Book s("edgar", "science", "chemistry for dummies", "502","12.11.13","1.12.96");
cout << "main finish" << endl;
return 0;
}