using namespace std;
class Student
{
public:
string m_strName;
int m_iAge;
};
int main()
{
Student *p = new stu;
stu.m_strName = "慕課網";
stu.m_iAge = 2;
cout << p -> m_strName << " " << p -> m_iAge<< endl;
delete p;
p = NULL;
return 0;
}
class Student
{
public:
string m_strName;
int m_iAge;
};
int main()
{
Student *p = new stu;
stu.m_strName = "慕課網";
stu.m_iAge = 2;
cout << p -> m_strName << " " << p -> m_iAge<< endl;
delete p;
p = NULL;
return 0;
}
確實有錯誤,p是指針,當一次在隊上申請了多個對象的話,p指向的是多個對象,->等價與(*).,即箭頭表示先解引用再運用點運算符對數據成員或成員函數進行調用,而p既然指向的是多個對象,那就可以用下標運算符對指針所指向的對象進行操作,就是說p[i]是p指向的某個對象,而非指針,而對象可以直接用點運算符對數據成員或成員函數進行調用,所以p[i]直接用點運算符即可。不過這套視頻講得挺好的,點贊
2016-07-23
00:44 時, 常量區的例子是否缺少了const? (const string str = "hello";)
2016-07-22