F_-_-_ 2021-08-21 14:04 采纳率: 100%
浏览 26
已结题

c++类和对象 使用嵌套构造函数初始化时的问题


#include<iostream>
#include<string>
using namespace std;

class card
{
public:
card(int a,string b)
{
index=a;
password=b;
}
int index;
string password;

};
class student
{
public:

1.  student(string a,int b,string c):m_name(a),m_card(b,c){}   //right

2.  student(string a,int b,string c)
{ 
m_name=a;
card m_card(b,c); //error
}

string m_name;
card m_card;
};


void f()
{
student hfj("hanfeijiang",0213,"hfj020709");
cout << hfj.m_name << endl;
cout << hfj.m_card.index << endl;
cout << hfj.m_card.password << endl;
}


int main()
{
f();
    return 0;
}

想问一下在class student中那两种student含参构造函数,为什么第一种直接使用初始化列标就正常赋值,第二种就错误。
img

  • 写回答

2条回答 默认 最新

查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月30日
  • 已采纳回答 8月22日
  • 创建了问题 8月21日