-
對象成員:組合概念的應(yīng)用
實例化的時候,先實例化被組合的部分,再實例化自身。析構(gòu)的時候剛好相反
查看全部 -
delete []xxx 是析構(gòu)數(shù)組所有對象
查看全部 -
常成員函數(shù)特殊點在于,在常成員函數(shù)中不能改變數(shù)據(jù)成員的值。? 常成員函數(shù)是常對象的使用函數(shù)。
查看全部 -
注意new和c語言中memory alloc的區(qū)別,后者只是單純的分配內(nèi)存,前者卻可以在這個過程中自動的調(diào)用構(gòu)造函數(shù),對內(nèi)存空間進(jìn)行結(jié)構(gòu)化處理。
查看全部 -
在類對象有指針時,簡單的拷貝構(gòu)造函數(shù)會將指針復(fù)制,導(dǎo)致兩個類的指針指向了相同區(qū)域,此時如果進(jìn)行類的內(nèi)存釋放delete,會對一個區(qū)域進(jìn)行兩次內(nèi)存釋放,從而引發(fā)崩潰。
為了解決這個問題,需要在復(fù)制構(gòu)造函數(shù)中指明,指針數(shù)組使用NEW來申請了一塊新的內(nèi)存,之后再用循環(huán)來將原數(shù)組指針指向的每一個值賦與新的指針
查看全部 -
對于復(fù)合類來說,子類的構(gòu)造函數(shù)中如果存在參數(shù),那么母類必須通過初始化列表的形式來對母類對象中的子類對象進(jìn)行賦值。如圖中的坐標(biāo)系類
查看全部 -
在使用new申請出類的數(shù)組內(nèi)存時,使用delete釋放內(nèi)存必須讓指針指向類數(shù)組的第一個對象位置
查看全部 -
#include <iostream>
using namespace std;
class Coordinate
{
?? ?
public:
?? ?Coordinate(int x, int y)
?? ?{
?? ??? ?// 設(shè)置X,Y的坐標(biāo)
?? ??? ?m_iX = x;
??????? m_iY = y;
?? ?}
??? // 實現(xiàn)常成員函數(shù)
?? ?void printInfo() const
?? ?{
?? ???? cout << "(" << m_iX << "," <<m_iY << ")" << endl;
?? ?}
public:
?? ?int m_iX;
?? ?int m_iY;
};
int main(void)
{
?? ?const Coordinate coor(3, 5);
?? ?// 創(chuàng)建常指針p
?? ?Coordinate const *p = &coor;
??? // 創(chuàng)建常引用c
??? Coordinate const &c = coor;
?? ?
?? ?coor.printInfo();
?? ?p->printInfo();
?? ?c.printInfo(); ?
?? ?
?? ?return 0;
}查看全部 -
#include <iostream>
using namespace std;
class Coordinate
{
?? ?
public:
?? ?Coordinate()
?? ?{
?? ??? ?
?? ?}
?? ?// 打印坐標(biāo)的函數(shù)
?? ?void printInfo() ?
?? ?{
?? ???? cout << "(" <<m_iX << "," << m_iY << ")" << endl;
?? ?}
public:
?? ?int m_iX;
?? ?int m_iY;
};
int main(void)
{
?? ?//定義對象數(shù)組
??? Coordinate *coorArr = new Coordinate[2];
??? coorArr->m_iX = 20;
??? coorArr[0].m_iY = 100;
??? coorArr++;
??? coorArr->m_iX = 40;
??? coorArr[0].m_iY = 30;
?? ?//遍歷數(shù)組,打印對象信息
?? ?for(int i = 0; i < 2; i++)
?? ?{
?? ??? ?coorArr->printInfo();
?? ??? ?coorArr--;
?? ?}?? ?
?? ?return 0;
}查看全部 -
#include <iostream>
using namespace std;
class Coordinate
{
?? ?
public:
?? ?Coordinate()
?? ?{
?? ?}
?? ?// 打印坐標(biāo)的函數(shù)
?? ?void printInfo() ?
?? ?{
?? ???? cout << "(" <<m_iX << "," << m_iY << ")" << endl;
?? ?}
public:
?? ?int m_iX;
?? ?int m_iY;
};
int main(void)
{
?? ?//定義對象數(shù)組
??? Coordinate coorArr[2];
??? coorArr[0].m_iX = 20;
??? coorArr[0].m_iY = 100;
??? coorArr[1].m_iX = 40;
??? coorArr[1].m_iY = 30;
?? ?//遍歷數(shù)組,打印對象信息
?? ?for(int i = 0; i < 2; i++)
?? ?{
?? ??? ?coorArr[i].printInfo();
?? ?}?? ?
?? ?return 0;
}查看全部 -
常成員函數(shù):
聲明:<類型標(biāo)志符>函數(shù)名(參數(shù)表)const;
如果一個成員函數(shù)對類中數(shù)據(jù)成員只作訪問而不作直接或間接的修改,則最好將此函數(shù)設(shè)置為常成員函數(shù),以明確表示它對數(shù)據(jù)成員的保護(hù)性。
必須是常對象才能調(diào)用常成員函數(shù)。
常成員函數(shù)與普通成員函數(shù)互為重載,常成員函數(shù)只能由常對象調(diào)用,普通成員函數(shù)由普通對象調(diào)用。
查看全部 -
!!!Array?
return *this;
返回的是一個臨時的新對象,之后對返回值的操作,不會影響到this原本指向的對象。
但是!
Array&?
return *this;
引用符號&的引入可以改變原this指向的對象.
查看全部 -
如果對象A中有對象成員B,對象B沒有默認(rèn)構(gòu)造函數(shù),那么對象A必須在初始化列表中初始化對象B(原因:因為實例化A時,會先執(zhí)行B的構(gòu)造函數(shù),再執(zhí)行A的構(gòu)造函數(shù),如若對象B沒有默認(rèn)構(gòu)造函數(shù),即需要給B的構(gòu)造函數(shù)傳遞參數(shù)才能調(diào)用,但是此時A的構(gòu)造函數(shù)還沒有執(zhí)行,因此它還拿不到A構(gòu)造函數(shù)的參數(shù),所以先調(diào)用B的構(gòu)造函數(shù)這個過程將無法進(jìn)行。而初始化列表會先于構(gòu)造函數(shù)的執(zhí)行對對象成員進(jìn)行初始化,因此不必再擔(dān)心B的構(gòu)造函數(shù)拿不到參數(shù)而無法執(zhí)行的問題。因此如果B沒有默認(rèn)構(gòu)造函數(shù),那么對象A必須在初始化列表中初始化對象B.)
轉(zhuǎn)自:https://blog.csdn.net/hudfang/article/details/50511481
查看全部 -
對象數(shù)組,this指針查看全部
-
一個對象可以有多個對象常引用
常成員函數(shù)只讀權(quán)限<普通成員函數(shù)讀寫權(quán)限
? ? - 普通對象能調(diào)用常成員函數(shù),也能調(diào)用普通成員函數(shù)
????- 常對象只能調(diào)用常成員函數(shù)
指向常對象的指針?= 常指針?= Coordinate const *pCoor;
指向?qū)ο蟮某V羔?= 指針常量?= Coordinate *const pCoor;
查看全部
舉報