|
|
发表于 2019-3-4 17:56:47
|
显示全部楼层
本楼为最佳答案
指针转换有问题,请注意注释:
- #include<iostream>
- #include<string>
- #include<memory> // 智能指针的头文件
- int main()
- {
- std::auto_ptr<std::string> pf[5]={ std::auto_ptr<std::string>(new std::string("@@@@@@@")) ,
- std::auto_ptr<std::string>(new std::string("#######")) ,
- std::auto_ptr<std::string>(new std::string("*************")) ,
- std::auto_ptr<std::string>(new std::string("%%%%%%%%%%")) ,
- std::auto_ptr<std::string>(new std::string("$")) };
- std::shared_ptr<std::string> pw(pf[2].get()); //将pf[2]转换为普通指针后,赋值给pw
- //pw = pf[2];
- std::cout<<"The nominees for best avian baseball fp are\n";
- for(int i=0; i<5; i++)
- {
- std::cout<<*pf[i]<<"\n";
- }
- std::cout<<"The winnwr is: "<<*pw<<"\n";
- std::cin.get();
- return 0;
- }
复制代码 |
|