C++學習筆記5_智慧指標
1. 一般的指標
int main(void)
{
int *p=new int;
*p=20;
delete p;
return 0;
}
智慧指標能自動回收
#include<memory> 記得要引用標頭檔案
int main(void)
{
//auto_ptr<int>模板寫法
auto_ptr<int> ptr(new int);
}
auto_ptr<>其實是一個模板類;
使用智慧指標,就不用自己delete了,也能自行呼叫解構函式
2.
class A
{
public :
A(int a)
{
//...
}
func()
{
//...
}
}
int main(void)
{
auto_ptr<A> ptr(new A(10));
ptr->func();
(*ptr).func();
//auto_ptr肯定過載了->和*操作符,並且在析構時,delelte了A的指標
}
相關推薦
C++學習筆記5_智慧指標
1. 一般的指標int main(void){ int *p=new int; *p=20; delete p; return 0;}智慧指標能自動回收#include<memory> 記得要引用標頭檔案int main(void){ //auto_ptr<int>模板寫法 auto_
C++學習筆記5_智能指針
ret 使用 delete 智能 模板 public del () let 1. 一般的指針int main(void){ int *p=new int; *p=20; delete p; return 0;}智能指針能自動回收#include<memory>
C#學習筆記201811_1_智慧感知圖示
2018年11月11日 10:00:27 starxyl 閱讀數:9 標籤: C# 學習筆記
學習筆記之智慧指標和執行緒安全內容筆記
使用shared_ptr控制物件的生命週期,常用來進行物件的建立,屬於強引用,只要被shared_ptr引用該物件就不會被析構 weak_ptr是一種弱引用,常常用來偵查物件是否存在,不控制物件的生命期,也不會增加物件的引用計數如果物件還存在沒被析構那麼可以通過成員函式進行
【C++學習筆記】詳解C++中的三種智慧指標
一、簡介 由於 C++ 語言沒有垃圾回收機制,程式設計師每次 new出來的記憶體都要手動 delete。程式設計師忘記 delete,有可能就會造成記憶體洩漏,程式崩潰等嚴重的後果。用智慧指標便可以有效緩解這類問題,本文主要講解常見的智慧指標的用法。包括:s
const修飾符、const與指標一起使用(C++學習筆記 6)
一、 在C++中使用const修飾符來定義常量。(const來自單詞constant,是常量的意思) 用法:const 型別 常量名 = 表示式; 例如:const int LIMIT = 100; 這個常量LIMIT是有型別的,佔用儲存單元,有地址,可以用指標指向它,但不能修改它。
指標(C++學習筆記 12)
記憶體將每個位元組作為一個儲存單元,並給每個儲存單元指定一個編號作為其地址。 系統給不同資料型別分配的儲存空間大小是不一樣的,如:整型變數分配2個位元組的儲存空間;double型變數分配8個位元組;字元型變數分配1個位元組。 變數的地址:因為一個變數的儲存空間要連續佔用若
指標與陣列(C++學習筆記 11)
一、指向陣列元素的指標 1、定義指向陣列元素的指標 每個陣列元素相當於一個變數,所以定義指向陣列元素的指標與定義指向變數的指標方法相同。例如: int ia[5]; /*定義包含5個數組元素的整型陣列ia*/ int *pi; /*定義指向整型變數的指標變數pi*/ p
C++學習筆記2_函式.函式指標
1. 行內函數void printAB(int a,int b){ cout<<(a)<<(b)<<endl;}int main(void){ for(int i=0;i<1000;i++) { int j=i+1; printAB(i,j
this 指標(C++學習筆記 22)
當定義了一個類的若干物件後,系統會為每一個物件分配儲存空間。 如果一個類包含了資料成員和成員函式,就要分別為資料和函式的程式碼分配儲存空間。 事實上,給物件賦值就是給物件的資料成員賦值,不同物件的儲存單元中存放的資料值通常是不同的,而不同物件的函式程式碼是相同的
物件指標(C++學習筆記 21)
物件指標就是用於存放物件地址的變數。 宣告物件指標的一般語法形式: 類名 *物件指標名 1、用指標訪問單個物件成員 在一般情況下,用點運算子 “ . ” 來訪問物件成員,當用指向物件的指標來訪問物件成員時,就要用 “ -> ” 操作符。 例 1: 物件指標的使用 #in
C-學習筆記6-指標基礎
1.變數的地址 在計算機中,記憶體是連續的儲存空間。為了便於對其中某個指定部分進行操作,要對記憶體進行編址,記憶體編址的基本單位為位元組。對於程式中定義的變數,編譯時根據它的型別給它分配一定程度的記憶體單元。分配給每個變數的記憶體單元的起始地址。編譯後每
C語言基礎學習筆記:day5 指標
注:本筆記為直接上傳,因各個markdown筆記語法的差異性,在顯示上略有區別。 如需原版請聯絡:[email protected]。(郵件主題為:學習筆記,正文需要的筆記名,可以直接複製該筆記的網址)。同時歡迎各位一起學習交流。 day5指標 文章目錄
【C++學習筆記】父類指標指向子類物件
虛擬函式的作用主要是實現了多型的機制。簡而言之就是用父型別的指標指向其子類的例項,然後通過父類的指標呼叫實際子類的成員函式。但僅僅可呼叫父類含有的函式,非父類函式不能呼叫。 普通虛擬函式呼叫 假設我們有下面的類層次: #includ
【 C/C++學習筆記整理】--1.常量的用法、指標與陣列的區別
巨集常量與const常量的區別: 指標與陣列的區別: ++i和i++的區別: 求X的n次冪 pow(X,n); 1.巨集常量與const常量的區別: 巨集常量,如 #define MAX_NUM 65536 本質為字
C++學習筆記(三)--函式引數,陣列函式,指標和const,二維陣列函式,遞迴,函式指標
C++ Primer Plus學習筆記之三 每一塊寫了一個詳細闡釋的demo,具體使用方法以及注意事項在程式碼裡都有備註 第七章函式--C++的模組程式設計,總計分為以下幾塊-- 函式引數
c++學習筆記 vptr指標的分佈和初始化
{public: Child(int a = 0, int b = 0) { this->b = b; } virtual void print() { cout << "我是兒子" << endl; }protected:private: int b;};void play
【C++學習筆記】函式匹配和函式指標
一、函式匹配 在大多數情況下,比較容易確定某次呼叫應選用哪個過載函式,然而當幾個過載函式的形引數量相等以及某些形參的型別可以由其他型別轉換得到時,這個相對就不那麼容易。 1.1 實參型別轉換 確定最佳匹配,編譯器將實參型別到形參型別的轉換分成以下
C++學習筆記--C語言模擬this指標
都知道,C++中類的成員變數和成員函式是分開儲存的,變數可以儲存在堆、棧、全域性區,而函式只能存在程式碼段,並且一個類只對應一套成員函式,那麼如何通過類物件呼叫成員函式呢? 答案是通過this指標,類物件將this指標傳遞給函式,所以函式能夠使用類物件的成員變數
c#學習筆記之Application.DoEvents應用
box nbsp net replace 運算 技術 oop blank 假死 Visual Studio裏的摘要:處理當前在消息隊列中的所有 Windows 消息。 交出CPU控制權,讓系統可以處理隊列中的所有Windows消息 比如在大運算量循環內,加Applicati