-
1.對(duì)象指針:類名 * 指針名 ?= new 類名 2.C語言的malloc與C++的new都具有申請(qǐng)新內(nèi)存空間的作用,但是new會(huì)調(diào)用對(duì)象的構(gòu)造函數(shù),而malloc不會(huì)調(diào)用 3. C++在new時(shí)的初始化的規(guī)律可能為:對(duì)于有構(gòu)造函數(shù)的類,不論有沒有括號(hào),都用構(gòu)造函數(shù)進(jìn)行初始化;如果沒有構(gòu)造函數(shù),則不加括號(hào)的new只分配內(nèi)存空間,不進(jìn)行內(nèi)存的初始化,而加了括號(hào)的new會(huì)在分配內(nèi)存的同時(shí)初始化為0。
查看全部 -
深拷貝實(shí)例
class Array { public: Array(int count); Array(const Array &arr); ~Array(); void setCount(int count); int getCount(); void printAddr(); void printArr(); private: int m_iCount; int *m_pArr; }; #include"Array.h" #include <iostream> using namespace std; Array::Array(int count) { m_iCount = count; m_pArr=new int[m_iCount]; for(int i=0;i<m_iCount;i++) { m_pArr[i]=i; } cout << "Array" <<endl; } Array::Array(const Array &arr) { m_iCount = arr.m_iCount; m_pArr =new int[m_iCount]; for(int i=0;i<m_iCount;i++) { m_pArr[i] = arr.m_pArr[i]; } cout <<"Array&" <<endl; } Array::~Array() { delete []m_pArr; m_pArr = NULL; cout <<"~Array" <<endl; } void Array::setCount(int count) { m_iCount = count; } int Array::getCount() { return m_iCount; } void Array::printAddr() { cout <<"m_pArr=" <<m_pArr <<endl; } void Array::printArr() { for(int i=0;i<m_iCount;i++) { cout << m_pArr[i] <<endl; } }
查看全部 -
class Array { public: Array(); Array(const Array &arr); ~Array(); void setCount(int count); int getCount(); private: int m_iCount; }; #include"Array.h" #include <iostream> using namespace std; Array::Array() { cout << "Array" <<endl; } Array::Array(const Array &arr) { m_iCount = arr.m_iCount; cout <<"Array&" <<endl; } Array::~Array() { cout <<"~Array" <<endl; } void Array::setCount(int count) { m_iCount = count; } int Array::getCount() { return m_iCount; } #include <iostream> #include <stdlib.h> #include "Array.h" using namespace std; int main() { Array arr1; arr1.setCount(5); Array arr2(arr1); cout << "arr2.m_iCount="<<arr2.getCount() <<endl; system("pause"); return 0; }
查看全部 -
淺拷貝:
將對(duì)象內(nèi)容簡單拷貝,指針成員指向同一地址
深拷貝:
在堆中開辟一塊新的地址,將原地址中的內(nèi)容拷貝進(jìn)去
查看全部 -
查看全部
-
因?yàn)槌跏蓟斜頃?huì)比構(gòu)造函數(shù)先執(zhí)行,再因?yàn)镃oordinate是Line的對(duì)象成員,會(huì)優(yōu)先于Line執(zhí)行構(gòu)造函數(shù),所以如果不把Coordinate的那兩個(gè)對(duì)象放在初始化列表中進(jìn)行初始化,將會(huì)導(dǎo)致Coordinnate的對(duì)象使用默認(rèn)構(gòu)造函數(shù)進(jìn)行初始化,然后你又沒有寫默認(rèn)的構(gòu)造函數(shù),所以會(huì)報(bào)錯(cuò),不過如果你寫了默認(rèn)構(gòu)造函數(shù),會(huì)導(dǎo)致多出來兩個(gè)對(duì)象。
查看全部 -
demo.cpp
#include<iostream>
#include<stdlib.h>
#include<string>
#include"Line.h"
using namespace std;
/*對(duì)象成員
要求:
? ? ?定義兩個(gè)類:
坐標(biāo)類:Coordinate
數(shù)據(jù)成員:m_iX和m_iY
成員函數(shù):構(gòu)造函數(shù),析構(gòu)函數(shù),數(shù)據(jù)封裝函數(shù)
線段類:Line
數(shù)據(jù)成員:點(diǎn)A m_coorA,點(diǎn)B m_coorB
成員函數(shù):構(gòu)造函數(shù),析構(gòu)函數(shù),數(shù)據(jù)封裝函數(shù),信息打印函數(shù)*/
int main(void)
{
Line *p = new Line();
delete p;
p = NULL;
system("pause");
return 0;
}
Line.cpp
#include"Line.h"
#include<iostream>
using namespace std;
Line::Line()
{
cout << "Line" << endl;
}
Line::~Line()
{
cout << "~Line" << endl;
}
void Line::setA(int x, int y)
{
m_coorA.setX(x);
m_coorA.setY(y);
}
void Line::setB(int x, int y)
{
m_coorB.setX(x);
m_coorB.setY(y);
}
void Line::printInfo()
{
cout << "(" << m_coorA.getX() << m_coorA.getY() << ")" << endl;
cout << "(" << m_coorB.getX() << m_coorB.getY() << ")" << endl;
}
Line.h#include"Coordinate.h"
class Line
{
public:
Line();
~Line();
void setA(int x,int y);
void setB(int x,int y);
void printInfo();
private:
Coordinate m_coorA;
Coordinate m_coorB;
};
Coordinate.cpp
#include"Coordinate.h"
#include<iostream>
using namespace std;
Coordinate::Coordinate()
{
cout << "Coordinate()" << endl;
}
Coordinate::~Coordinate()
{
cout << "~Coordinate()" << endl;
}
void Coordinate::setX(int x)
{
m_iX = x;
}
int Coordinate::getX()
{
return m_iX;
}
void Coordinate::setY(int y)
{
m_iY = y;
}
int Coordinate::getY()
{
return m_iY;
}
Coordinate.h
class Coordinate
{
public:
Coordinate();
~Coordinate();
void setX(int x);
int getX();
void setY(int y);
int getY();
private:
int m_iX;
int m_iY;
};
查看全部 -
對(duì)象成員:一個(gè)對(duì)象中包含其他對(duì)象 如:class Line{ ? ?public: ? ? ?Line(); ? ?private: ? ? ?Coordinate m_coorA; ? ? ?Coordinate m_coorB; } 當(dāng)實(shí)例化這樣一個(gè)對(duì)象時(shí),會(huì)先實(shí)例化m_coorA,再實(shí)例化m_coorB,最后實(shí)例化Line 對(duì)象消亡時(shí),會(huì)先銷毀Line,再m_coorB,最后m_coorA
查看全部 -
棧中內(nèi)存被銷毀是在整個(gè)main函數(shù)執(zhí)行完后銷毀; 而堆中由指針P所指向的內(nèi)存是可手動(dòng)控制何時(shí)銷毀(利用delete )可通過此來顯示析構(gòu)函數(shù)的存在;
查看全部 -
p[0]指向第一個(gè)元素;執(zhí)行p++之后p[0]指向第2個(gè)了!?。?lt;br>
2.釋放內(nèi)存時(shí)要注意指針 p 指回原來的位置。用delete釋放數(shù)組內(nèi)存時(shí)要注意此時(shí)的指針*p要指到該數(shù)組的第一個(gè)元素上。保證申請(qǐng)和釋放的是同一段內(nèi)存第二個(gè)循環(huán)第一次打印的是第三個(gè)元素因?yàn)橹羔樈?jīng)過p++到第三個(gè)元素了,所以得用p--
注意:在對(duì)象數(shù)組中,想要訪問某個(gè)堆中的對(duì)象的成員屬性時(shí),p->m_ix=1;和p[0].m_ix=1效果是一樣的,但是p[0]->m_ix=1這種用法是錯(cuò)誤、以及注意 p++后 p[0] 前后的指向是不一樣的
數(shù)組的頭指針最好要保留,采用(p+1)->m_iX的方式訪問,不會(huì)更改頭指針
查看全部 -
首先,能用拍p[2]->m_iY=20 給第三個(gè)賦值嗎? ? ? ? ? ? ? 是不不不可以的?。。?! 還有,對(duì)于它——[],下標(biāo)運(yùn)算符,是這樣處理的:p[2]等價(jià)于*(p+2),代表第三個(gè)元素實(shí)體本身. ? ?而 ?-> ?這個(gè)運(yùn)算符是與指針(即地址)搭配使用的。即(p+2)->m_iY=20,是可以滴。 其實(shí),對(duì)于學(xué)過的數(shù)組,a[i]那啥的,也是這樣處理的:*(a+i),不過a代表數(shù)組名,是個(gè)常量而已。
查看全部 -
C++路線: 起航-->離港-->封裝-->繼承
查看全部 -
#include?<iostream> #include?<Windows.h> #include?<string> #include?<process.h> #include?<Windows.h> using?namespace?std; /*?https://zhidao.baidu.com/question/1755742141667975828.html? 設(shè)定初始面向?East?(?如果是右手抹墻則是?West) 本代碼以右手摸墻為例。 1如果當(dāng)前格為第一排,則說明當(dāng)前格為終點(diǎn),結(jié)束。 2根據(jù)當(dāng)前格的數(shù)據(jù),找到下一步需要面向的方向,?方法是,如果當(dāng)前方向上有墻,則逆時(shí)針(左手抹墻則順時(shí)針)轉(zhuǎn)身,重復(fù)這一步驟直到面向的方向上可以行走 3沿當(dāng)前方向走一步 4順時(shí)針(左手抹墻則逆時(shí)針)轉(zhuǎn)身一次,使當(dāng)前面對(duì)方向?yàn)榈?步之后的游手(或左手)方向,?然后回到步驟1? ????????[這樣每走一步之后轉(zhuǎn)身一次,是為了保證摸墻可以一直靠著墻走,這個(gè)方式非常好,就不用去每次判斷你的右手邊是不是有路, 因?yàn)槊孔咭徊街竽愣枷蛴肄D(zhuǎn),這樣你之前的右方就變成了前方,你只需要判斷前方有沒有路??!] */ //墻體和路常量 #define?WALL??"■" #define?ROAD??"??" //迷宮大小常量 const?int?X?=?8; const?int?Y?=?8; //方向枚舉 enum?direction{ N?=?0, S?=?1, W?=?2, E?=?3 }; //地圖類 class?MazeMap?{ public: MazeMap()?{} void?coutMap(string?map[X][Y])?{?//構(gòu)造地圖 for?(int?i?=?0;?i?<?X;?i++)?{ for?(int?j?=?0;?j?<?Y;?j++)?{ cout?<<?map[i][j]?; } cout?<<?endl; } } }; //人類類 class?Human?{ public: Human(string?_man)?:x(6),?y(6),?man_signal(_man),?direction(W){}?//初始化人物位置和圖標(biāo) //橫縱坐標(biāo) int?x; int?y; //人物顯示標(biāo)識(shí) string?man_signal; //方向 int?direction; //?移動(dòng)方法 void?humanMove(string?map[X][Y],?Human?*man)?{ MazeMap?*mazeMap?=?new?MazeMap; map[man->x][man->y]?=?man->man_signal; mazeMap->coutMap(map); cout?<<?"READY?(y/n)"?<<?endl; char?flag?=?'n'; cin?>>?flag; //單向?qū)ぢ吩瓌t??右手摸墻,初始朝向?yàn)閃 if?(flag?==?'y')?{ do { turnLeft(map,?man); move(map,?man); Sleep(1000); system("cls");//清屏作用,動(dòng)畫的移動(dòng)靠每一次一幀一幀的畫面來實(shí)現(xiàn)。 mazeMap->coutMap(map);//清屏之后重繪迷宮,動(dòng)畫的移動(dòng)靠每一次一幀一幀的畫面來實(shí)現(xiàn)。 }?while?(finsh(man)); cout?<<?"YOU?WIN!!!"?<<?endl; } else?{ cout?<<?"YOU?ARE?A?LOSE!!!"?<<?endl; } delete?mazeMap; mazeMap?=?NULL; } //右手摸墻原則:?如果正前方方向上有墻就逆時(shí)針轉(zhuǎn)一下(向左轉(zhuǎn)90°) void?turnLeft(string?map[X][Y],Human?*man)?{ if?(man->direction?==?N)?{ if?(map[man->x?-?1][man->y]?==?WALL)?{ man->direction?=?W; turnLeft(map,?man);//循環(huán),不停的左轉(zhuǎn)直到找到前方不是墻 } return; } if?(man->direction?==?S)?{ if?(map[man->x+1][man->y]?==?WALL)?{ man->direction?=?E; turnLeft(map,?man);//循環(huán),不停的左轉(zhuǎn)直到找到前方不是墻 } return; } if?(man->direction?==?W)?{ if?(map[man->x][man->y-1]?==?WALL)?{ man->direction?=?S; turnLeft(map,?man);//循環(huán),不停的左轉(zhuǎn)直到找到前方不是墻 } return; } if?(man->direction?==?E)?{ if?(map[man->x][man->y+1]?==?WALL)?{ man->direction?=?N; turnLeft(map,?man);//循環(huán),不停的左轉(zhuǎn)直到找到前方不是墻 } return; } } //移動(dòng)一格??后順時(shí)針調(diào)轉(zhuǎn)方向(每次向前走一步之后,都把方向右轉(zhuǎn)90° ??為了保證右手摸墻原則,每次都往右邊靠) void?move(string?map[X][Y],?Human?*man)?{ if?(man->direction?==?N)?{ map[man->x?-?1][man->y]?=?man->man_signal; map[man->x][man->y]?=?ROAD; man->x?-=?1; man->direction?=?E; }else?if?(man->direction?==?S)?{ map[man->x?+?1][man->y]?=?man->man_signal; map[man->x][man->y]?=?ROAD; man->x?+=?1; man->direction?=?W; }else?if?(man->direction?==?W)?{ map[man->x][man->y?-?1]?=?man->man_signal; map[man->x][man->y]?=?ROAD; man->y?-=?1; man->direction?=?N; }else?if(man->direction?==?E)?{ map[man->x][man->y?+?1]?=?man->man_signal; map[man->x][man->y]?=?ROAD; man->y?+=?1; man->direction?=?S; } return; } //判斷是否完成 bool?finsh(Human?*man)?{ if?(man->x?==?0) return?false; return?true; } }; int?main(void)?{ string?map[X][Y]?=?{?{WALL,ROAD,WALL,WALL,WALL,WALL,WALL,WALL}, {WALL,ROAD,ROAD,ROAD,ROAD,ROAD,WALL,WALL}, {WALL,WALL,WALL,WALL,WALL,ROAD,ROAD,WALL}, {WALL,ROAD,ROAD,ROAD,ROAD,ROAD,WALL,WALL}, {WALL,WALL,ROAD,WALL,ROAD,ROAD,WALL,WALL}, {WALL,WALL,ROAD,ROAD,ROAD,WALL,WALL,WALL}, {WALL,WALL,WALL,WALL,ROAD,ROAD,ROAD,WALL}, {WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL}?}; Human?*man?=?new?Human("⊙"); man->humanMove(map,man); delete?man; man?=?NULL; return?0; }
查看全部 -
結(jié)構(gòu)里面有指針,只拷貝了指針一個(gè)道理。
在C++里面還發(fā)明一個(gè)兩個(gè)概念,深淺拷貝。
查看全部 -
一個(gè)對(duì)象的里面的對(duì)象成員的銷毀順序:先構(gòu)建的后銷毀。類似于棧
查看全部
舉報(bào)