指针(二)
访问数据成员
本来的语法应该是:
(*pRags).GetAge();
因为这样比较麻烦,所以有简略的语法:
pRags->GetAge();
自由存储区中的成员数据
类可能有一个或多个数据成员是自由存储区中的对象指针。内存可有类构造函数或某个方法来分配,由析构函数来释放。例子:
#include <iostream>
using namespace std;
class SimpleCat
{
public:
SimpleCat();
~SimpleCat();
int GetAge() const {return *itsAge;}
void SetAge(int age) {*itsAge = age;}
int GetWeight() const {return *itsWeight;}
void SetWeight(int weight) {*itsWeight = weight;}
private:
int *itsAge;
int *itsWeight;
};
SimpleCat::SimpleCat() {
itsAge = new int(2);
itsWeight = new int(5);
}
SimpleCat::~SimpleCat() {
delete itsAge;
delete itsWeight;
}
int main(int argc, char *argv[])
{
SimpleCat* Frisky = new SimpleCat;
cout << "Frisky is " << Frisky->GetAge() << " years old\n";
Frisky->SetAge(5);
cout << "Frisky is " << Frisky->GetAge() << " years old\n";
delete Frisky;
return 0;
}
using namespace std;
class SimpleCat
{
public:
SimpleCat();
~SimpleCat();
int GetAge() const {return *itsAge;}
void SetAge(int age) {*itsAge = age;}
int GetWeight() const {return *itsWeight;}
void SetWeight(int weight) {*itsWeight = weight;}
private:
int *itsAge;
int *itsWeight;
};
SimpleCat::SimpleCat() {
itsAge = new int(2);
itsWeight = new int(5);
}
SimpleCat::~SimpleCat() {
delete itsAge;
delete itsWeight;
}
int main(int argc, char *argv[])
{
SimpleCat* Frisky = new SimpleCat;
cout << "Frisky is " << Frisky->GetAge() << " years old\n";
Frisky->SetAge(5);
cout << "Frisky is " << Frisky->GetAge() << " years old\n";
delete Frisky;
return 0;
}
this 指针,例子:
#include <iostream>
using namespace std;
// 例子:使用 this 指针
class Rectangle
{
public:
Rectangle();
~Rectangle();
void SetLength(int length) {this->itsLength = length;}
int GetLength() const {return this->itsLength;}
void SetWidth(int width) {this->itsWidth = width;}
int GetWidth() const {return this->itsWidth;}
private:
int itsLength;
int itsWidth;
};
// 注意这个构造函数语法!
Rectangle::Rectangle():
itsWidth(5),
itsLength(10)
{ }
Rectangle::~Rectangle() {}
int main(int argc, char *argv[])
{
Rectangle theRect;
cout << "theRect is " << theRect.GetLength() << " feet long.\n";
cout << "theRect is " << theRect.GetWidth() << " feet wide.\n";
theRect.SetLength(20);
theRect.SetWidth(10);
cout << "theRect is " << theRect.GetLength() << " feet long.\n";
cout << "theRect is " << theRect.GetWidth() << " feet wide.\n";
return 0;
}
using namespace std;
// 例子:使用 this 指针
class Rectangle
{
public:
Rectangle();
~Rectangle();
void SetLength(int length) {this->itsLength = length;}
int GetLength() const {return this->itsLength;}
void SetWidth(int width) {this->itsWidth = width;}
int GetWidth() const {return this->itsWidth;}
private:
int itsLength;
int itsWidth;
};
// 注意这个构造函数语法!
Rectangle::Rectangle():
itsWidth(5),
itsLength(10)
{ }
Rectangle::~Rectangle() {}
int main(int argc, char *argv[])
{
Rectangle theRect;
cout << "theRect is " << theRect.GetLength() << " feet long.\n";
cout << "theRect is " << theRect.GetWidth() << " feet wide.\n";
theRect.SetLength(20);
theRect.SetWidth(10);
cout << "theRect is " << theRect.GetLength() << " feet long.\n";
cout << "theRect is " << theRect.GetWidth() << " feet wide.\n";
return 0;
}