《c++入門經典》筆記11 第十一章 開發高級指針 11.1在堆中創建對象 實際上,類就是對象的類型,對象也是一種變數,所以你可以在堆中創建int型變數,自然也就能創建自定義型變數。 Cat *pCat = new Cat; 這將調用預設構造函數(無參構造函數),每當在堆或棧中創建對象時,都將調用構 ...
《c++入門經典》筆記11
第十一章 開發高級指針
11.1在堆中創建對象
實際上,類就是對象的類型,對象也是一種變數,所以你可以在堆中創建int型變數,自然也就能創建自定義型變數。
Cat *pCat = new Cat;
這將調用預設構造函數(無參構造函數),每當在堆或棧中創建對象時,都將調用構造函數。
11.2刪除對象
對指向堆中對象的指針調用delete時,將調用對象的析構函數,然後釋放記憶體。
程式清單11.1 HeapCreator.cpp
#include <iostream>
class SimpleCat
{
public:
SimpleCat()
{
std::cout << "Constructor called\n";
itsAge = 1;
}
~SimpleCat()
{
std::cout << "Destructor called\n";
}
private:
int itsAge;
};
int main()
{
std::cout << "SimpleCat simpleCat ...\n";
SimpleCat simpleCat;
std::cout << "SimpleCat *pRags = new SimpleCat ...\n";
SimpleCat *pRags = new SimpleCat;
std::cout << "delete pRags ...\n";
delete pRags;
std::cout << "Exiting, watch simpleCat go ...\n";
return 0;
}
這裡最後一個Destructor called是因為main()函數結束時,simpleCat對象不再在作用域中,所以編譯器調用其析構函數。
11.3使用指針訪問成員
方法一(解引用運算符):
(*pRags).getAge();
方法二(指向運算符->):
pRags->getAge();
程式清單11.2 HeapAccessor.cpp
#include <iostream>
class SimpleCat
{
public:
SimpleCat()
{
itsAge = 2;
}
~SimpleCat()
{
std::cout << "Destructor called\n";
}
int getAge() const { return itsAge; }
void setAge(int age) { itsAge = age; }
private:
int itsAge;
};
int main()
{
SimpleCat *simpleCat = new SimpleCat;
std::cout << "simpleCat is " << (*simpleCat).getAge() << " years old"
<< "\n";
simpleCat->setAge(5);
std::cout << "simpleCat is " << simpleCat->getAge() << " years old"
<< "\n";
return 0;
}
11.4堆中的數據成員
類可能有一個或多個數據成員為指針,並指向堆中的對象。可在構造函數或成員函數中分配記憶體,併在析構函數中釋放記憶體。
程式清單11.3 DataMember.cpp
#include <iostream>
class SimpleCat
{
public:
SimpleCat()
{
itsAge = new int(2);
itsWeight = new int(5);
}
~SimpleCat()
{
delete itsAge;
delete itsWeight;
}
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;
};
int main()
{
SimpleCat *simpleCat = new SimpleCat;
std::cout << "simpleCat is " << simpleCat->getAge() << " years old"
<< "\n";
simpleCat->setAge(5);
std::cout << "simpleCat is " << simpleCat->getAge() << " years old"
<< "\n";
return 0;
}
11.5this指針
每個類成員函數都有一個隱藏的參數——this指針,它指向用於調用函數的對象。
通常,在成員函數中,無需使用this指針來訪問當前對象的成員變數,如果願意,可以顯示地使用this指針。
程式清單11.4 This.cpp
#include <iostream>
class Rectangle
{
private:
int itsLength;
int itsWidth;
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; }
};
Rectangle::Rectangle()
{
itsWidth = 5;
itsLength = 10;
}
Rectangle::~Rectangle()
{
}
int main()
{
Rectangle theRect;
std::cout << "theRect is " << theRect.getLength() << " feet long." << std::endl;
std::cout << "theRect is " << theRect.getWidth() << " feet wide." << std::endl;
theRect.setLength(20);
theRect.setWidth(10);
std::cout << "theRect is " << theRect.getLength() << " feet long." << std::endl;
std::cout << "theRect is " << theRect.getWidth() << " feet wide." << std::endl;
return 0;
}
11.6懸垂指針
懸垂指針又稱為野指針或者迷失指針,指的是對指針調用了delete(釋放其指向的記憶體)之後,沒有重新賦值(即沒有重新初始化)就開始被使用的指針。
實際上上章筆記中delete關鍵字時就已經提到野指針的危害。所以進行delete之後應該重新new賦值或者設置為nullptr。
11.7const指針
聲明指針時,可在類型前、類型後或者兩個地方都使用const。
const int *pOne;//指向常量的指針 int * const pTwo;//常量指針 const int * const pThree;//指向常量的常量指針
三條語句意義各不相同,三個指針類型也各不相同。
pOne是指向整型常量的指針,也就是編譯器預設它指向的是一個常量(雖然可能不是),所以不能通過這個指針來更改所指向的常量(編譯器認為是常量但不一定是)的值,比如
*pOne = 5;
編譯器就會報錯。int one = 10; const int * pOne = &one; *pOne = 5;//報錯,表達式必須是可修改的左值,但此時*pOne被認為不可修改
pTwo是指向整型的常量指針,可以修改指向的整型變數,但是pTwo不能指向其他變數。
int two = 20; int * const pTwo = &two; *pTwo = 15; pTwo = &one;//報錯,不能指向別的變數
pThree是一個指向整型常量的常量指針,不能修改它指向的值,也不能讓它指向其他變數。
int three = 30; const int * const pThree = &three; pThree = &one;//報錯,不能指向別的變數 *pThree = 25;//報錯,此時*pThree被認為不可修改
完整代碼:(註釋起來的是報錯的)
#include <iostream>
int main()
{
int one = 10;
const int * pOne = &one;
// *pOne = 5;
int two = 20;
int * const pTwo = &two;
*pTwo = 15;
// pTwo = &one;
int three = 30;
const int * const pThree = &three;
// pThree = &one;
// *pThree = 25;
std::cout<<"one: "<<one<<" *pOne: "<<*pOne<<std::endl;
std::cout<<"two: "<<two<<" *pTwo: "<<*pTwo<<std::endl;
std::cout<<"three: "<<three<<" *pThree: "<<*pThree<<std::endl;
return 0;
}
11.8const指針與const成員函數
程式清單11.5 ConstPointer.cpp
#include <iostream>
class Rectangle
{
private:
int itsLength;
int itsWidth;
public:
Rectangle();
~Rectangle();
void setLength(int length) { itsLength = length; }
int getLength() const { return itsLength; }
void setWidth(int width) { itsWidth = width; }
int getWidth() const { return itsWidth; }
};
Rectangle::Rectangle() : itsWidth(5), itsLength(10) //初始化列表
{
}
Rectangle::~Rectangle() {}
int main()
{
Rectangle *pRect = new Rectangle;
const Rectangle *pConstRect = new Rectangle; //pConstRect為指向Rectangle常量型對象的指針
Rectangle *const pConstPtr = new Rectangle; //pConstPtr為指向Rectangle型對象的常量指針
std::cout << "pRect width: " << pRect->getWidth() << " feet\n";
std::cout << "pConstRect width: " << pConstRect->getWidth() << " feet\n";
std::cout << "pConstPtr width: " << pConstPtr->getWidth() << " feet\n";
pRect->setWidth(10);
//pConstRect->setWidth(10);
pConstPtr->setWidth(10);
std::cout << "pRect width: " << pRect->getWidth() << " feet\n";
std::cout << "pConstRect width: " << pConstRect->getWidth() << " feet\n";
std::cout << "pConstPtr width: " << pConstPtr->getWidth() << " feet\n";
return 0;
}