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