構造函數初始化問題
為什么我在編譯時 , Student st1可以完成初始化 而Student st1()卻不能初始化呢?
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
class Teacher {
public:
Teacher() {
m_strName = "jame";
m_iAge = 16;
cout << "已經成功了!" << endl;
}
Teacher(string name, int age) {
m_strName = name;
m_iAge = age;
cout << "哈哈 你錯了吧!" << endl;
}
string getName() {
return m_strName;
};
int getAge() {
return m_iAge;
};
private:
string m_strName;
int m_iAge;
?};
int main(void) {
Teacher st1;
//Teacher st2();\Teacher("jame",16);
Teacher *p=NULL;
p = new Teacher();
cout << p->getName() << " " << p->getAge() << endl;
delete p;
p = NULL;
system("pause");
return 0;
}
2017-08-14
實例化對象 ①是從棧中實例化?? ②是從堆中實例化?? 從堆中實例化一個對象時才會要()
2017-11-07
明明是Teacher?
2017-08-08
Student stu=new Student();
或者Student stu;