[精讚] [會員登入]
343

【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

你可能感興趣的文章

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

【Firefox \ Maven】[Headless](Linux) 如何使用Maven打包並驅動Firefox 有時候我們需要取得渲染後的網頁,當然是直接呼叫瀏覽器出來用啦

【Wicket】[nonce]如何導入css以及javascript 如果只是用一般的 HTML link 或是 script 標籤來寫,會被 CSP 擋住,禁止加載資源,那我們除了關閉 CSP 以外還有沒有辦法來解決呢?

【Maven】[Wicket](Java) 如何部署Wicket到Wildfly內 Wicket 是一個 Java 下的套件,換言之,我們的網頁伺服器後端是由 Java 來撰寫的

【C++】一些好用的C++小功能 —— 貳 承襲上一篇的C++小功能,筆記下來以免自己以後忘記了。

【Discord bot 2.0.1】(discord.ui.View)如何將舊機器人升級至目前最新版本 很多時候升級軟體是為了讓別人覺得你的程式很先進,但這次卻是因為要使用某個新功能

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

高捷少女:耐耐的新年驚喜③ 即使如此,夫人仍然每年都會問老爺是否能空出一週時間,但總是被回絕,除了前年以外。老爺答應夫人一定會排出空檔,他們在去年的二十三日前往澳洲。」耐耐嘆了口氣。「那一天的晚上,我打電話給媽媽時,她很高興地告

婕兒──她的青春② 艾米直搖頭。「我真不敢相信,小穹妳都二十幾歲了,為什麼能想出這種故事呀?」「婕兒也是二十幾歲啊,妳想想她現在是什麼樣子?」小穹不滿地指向火車的方向。

【歌評】蓮台野夜行 - 幻想的永遠祭 蓮台野的探險雖然結束了,但是,誰知道是不是一段新的探險的序曲呢?

【日翻中歌詞】ピースサイン(Peace Sign) 那一天從我們的頭頂 輕掠而過的那架飛機 有點不可思議 還記得 為何呢?毫無意義 

艾米莉亞和高捷戀旅③ 「妳最好給我一個完整的理由,告訴我妳為什麼要這麼做。」艾米雙手叉腰,看著這位冒名參賽的後輩。「我會根據妳的說詞來判定妳違反規定的懲處。」