结构体初始化时,用一个函数赋值为啥会出毛病呢,求帮忙
#include <iostream>
#include <string>
using namespace std;
struct free_throws
{
string name;
};
const free_throws & clone2(free_throws &ft){
free_throws newguy; //创建了临时变量
newguy = ft;
return newguy;
const free_throws & clone3(free_throws &ft){
free_throws * newguy; //创建了临时变量
*newguy = ft;
return *newguy;
}
int main(){
struct free_throws ft_{"abc"};
cout << "ft_ address = " << &ft_<< endl;
// const struct free_throws ft_2 = clone2(ft_);
const struct free_throws ft_2 = clone3(ft_);
// struct free_throws ft_2;
// ft_2 = clone3(ft_);
cout << "ft2_ address = " << &ft_2<<endl;
}
现在的代码,只打印了一行
如果这样写
struct free_throws ft_{"abc"};
cout << "ft_ address = " << &ft_<< endl;
// const struct free_throws ft_2 = clone2(ft_);
// struct free_throws ft_2 = clone3(ft_);
struct free_throws ft_2;
ft_2 = clone3(ft_);
cout << "ft2_ address = " << &ft_2<<endl;
return 0;
为啥就可以打印两行呢
求解惑