[精讚] [會員登入]
377

【C++】一些好用的C++小功能 —— 貳

承襲上一篇的C++小功能,筆記下來以免自己以後忘記了。

分享此文連結 //n.sfs.tw/14954

分享連結 【C++】一些好用的C++小功能 —— 貳@小編過路君子
(文章歡迎轉載,務必尊重版權註明連結來源)
2021-03-13 16:39:02 最後編修
2021-03-02 23:15:06 By 過路君子
 

哈囉大家好,這裡是筆記了一堆用不到程式的小編過路君子

怕哪天自己要用找不到,特此記錄下來,一些比較稀奇的用法

 

上一篇的筆記在這裡!基本上跟這一篇一樣!

大標題、完整程式碼、如果有輸出會附上輸出,基本上等於0的講解!

希望能在這些非常雜亂的文章裡面找到你所需要的小功能,就算不會用看過至少也有個C++能這樣寫的概念吧~

 

以指標的形式呼叫函數

#include<iostream>

using namespace std;

void out(int);
void add(int, int, void (*op)(int));	//一定要先宣告

int main()
{
	int a = 9, b = 6;
	add(a, b, out);
	
	return 0;
}

void add(int ui, int ux, void (*op)(int br))
{
	ui += ux;
	(*op)(ui);
}

void out(int xc)
{
	cout << xc;
}

輸出

15

 

呼叫被區域變數遮蔽的全域變數

#include<iostream>
#include<string>
using namespace std;

string apple("yummy!\n");

int main()
{
	string apple("yuck!\n");
	cout << "Apple is so " << apple;
	cout << "Umm...no, is so " << ::apple;
	return 0;
}

輸出

Apple is so yuck!
Umm...no, is so yummy!

 

建立區域靜態變數
~再次呼叫副程式時不會被初始化

#include<iostream>
using namespace std;

void call(void);

int main()
{
	for(int ui=0;4>ui;ui++) call();
	return 0;
}

void call(void)
{
	static int a = 0;
	cout << "Now number is " << a << '\n';
	a += 2;
}

輸出

Now number is 0
Now number is 2
Now number is 4
Now number is 6

 

函數重載和呼叫、初始化隱藏於class內成員

#include<iostream>
#include<string>
using namespace std;

class vov
{
private:
	string ui;
	int ux;

public:
	vov(string);
	void out(void);
	void out(int);
};

int main()
{
	vov Alice("Yale");	//函數初始化
	Alice.out();
	Alice.out(9);		//函數重載
	return 0;
}

vov::vov(string init)
{
	this->ui = init;
	this->ux = 6491;
}

void vov::out(void)
{
	cout << this->ui << ' ' << this->ux << '\n';
}

void vov::out(int add)
{
	this->ux += add;
	cout << this->ui << ' ' << this->ux << '\n';
}

輸出

Yale 6491
Yale 6500

 

使所有的class共用某項變數

#include<iostream>
using namespace std;

class test
{
public:
	static int ux;
};

int test::ux;	//一定要宣告!!! 

int main()
{
	test op;
	op.ux = 777;
	
	test some;	//宣告新的test物件
	cout << some.ux << '\n';
	//宣告於class內的static (type)可以想像是存在於這個class內的全域變數
	return 0;
}

輸出

777

 

class物件之間的轉換

#include<iostream>
#include<string>

using namespace std;

class reset
{
private:
	string game;
	int number;
	
public:
	reset(string);
	
	void SetNumber(int);
	
	operator int() const;
	operator string() const;
};

int main()
{
	reset kill("prayer");
	kill.SetNumber(777);
	
	int a = 23;
	a += kill;
	cout << a << '\n';

	string b = kill;
	cout << b << '\n';
	
	return 0;
}

reset::reset(string gameName)
{
	this->game = gameName;
	this->number = 0;
}

void reset::SetNumber(int num)
{
	this->number = num;
}

reset::operator int() const
{
	return this->number;
}

reset::operator string() const
{
	string information = "\'s game";
	return this->game + information;
}

輸出

800
prayer's game

 

建構複製運算子

#include<iostream>
using namespace std;
 
class reset
{
private:
    int *number;
     
public:
    reset(int *);
    reset(const reset *);	//複製建構子

	void out(void);   
};
 
int main()
{
	int *x = new int[5];
	for(int a=0, b=2;5>a;a++,b+=9) x[a] = b;
    reset *kill = new reset(x);
	delete [] x;

	reset pray(kill);
	delete kill;	//證明pray的number不是指向kill的number
	pray.out();
     
    return 0;
}
 
reset::reset(int *nums)
{
    this->number = new int[5];
	for(int a=0;5>a;a++) this->number[a] = nums[a];
}

reset::reset(const reset *oldReset)
{
	this->number = new int[5];
	for(int a=0;5>a;a++) this->number[a] = oldReset->number[a];
}

void reset::out(void)
{
	for(int a=0;5>a;a++) cout << this->number[a] << ' ';
}

輸出

2 11 20 29 38

 

END

你可能感興趣的文章

【Arduino/LinkIt 7697】實作小小的 MQTT Publish & Subscribe 若是要多點對多點傳輸資料,那使用 MQTT 即可快速的達成我們所需的目的

【Wicket】[CSP] Content-Security-Policy & Content-Security-Policy-Report-Only Wicket 預設開啟的,所以如果要加載外部資源甚至是同源資源都會被擋下。

【Maven】如何創建一個簡單可部屬的WAR檔 滿重要的一個大功能,在使用JAVA網頁伺服器的時候一定會需要這個WAR檔來進行部屬

【Wicket】[Cookie]如何讀取和設定客戶端的Cookie 對於某些參數需要給予使用者,我們可以使用 Cookie 來讓客戶端記著,之後再跟伺服器裡的比對來達到驗證的目的

【C++】使用SFML製作讓方塊落下的畫面 從開啟新視窗延伸過來的應用(?),配合上一篇所使用到開啟一個可渲染視窗的那堆程式碼的延伸。

【Socket】(Linux / Python 3)兩不同系統的主機之間如何使用Socket相互溝通 How to connect two different computer use Socket

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

高捷少女:小穹與果仁巧克力㊤ 阿敏突然輕笑一聲,從櫃臺拿來一個塑膠餐盒,打開給大家看。「這是小穹烤的餅乾,妳們吃吃看就知道她為什麼不想講了。」小穹變得緊張起來。「阿敏,妳怎麼還留著呀?」艾米莉亞、婕兒與耐耐各自拿了一塊,把夾著奶油

高捷少女:地下城的探險少女① 婕兒心中一奇,便走上前看著仔細。那塊凹進去的地方中心大約三公分厚,越往邊緣就越淺,圓型直徑十五公分。婕兒拿出銅盤對比一下,發現兩者大小竟然一致,銅盤似乎能夠完整的嵌進去。     婕兒看著凹槽,心中

高捷少女:地下城的探險少女⑤ 小穹寫好後,耐耐看了一遍。「我想我應該辦得到。」她說完跪坐在地上,然後把古箏安放在大腿上,並將樂譜放在前面的地板。「要開始囉。」耐耐閉上眼睛,深呼吸一口氣,小穹等人在一旁看著她,心中暗自替她打氣。

婕兒──她的青春① 「各位乘客,本列車即將抵達拉里奧哈自治區,並在此地停留三天兩夜,後天的中午十二點將搭乘班機返回臺灣,感謝各位乘客對本次旅程的配合。」火車上的廣播器朗誦道。「時間過得真快呢,這次的歐洲之旅就這樣結束了,

【英翻中歌詞】(二創歌)Reincarnation “Well, hello there. It’s been a while…” 「喔,你好啊!好久不見了...」 Sealed in a sacred shrine… 被封印在神社之中... Ba