目前分類:[技術分享] (126)
- Apr 15 Fri 2022 10:37
[教學]FFmpeg - Audio - PCM to WAV
- Apr 13 Wed 2022 17:09
[教學]FFmpeg - Audio - PCM to AAC
- Feb 22 Tue 2022 17:37
[教學]C++ 函數指針,函數模板
/**
*
* C++ 中的函數指針模板
*
* 所謂的函數指針模板就是指向函數模板的函數指針.
*
*[問題描述]
* 定義了一類函數模板, 而且這函數模板有共同的街口, 即一致的參數列表, 那麼如何定義一個函數指針,
* 使這個函數指針可以指向這一類中的所有函數模板呢?
*
* [一]
* 我們先了解一件事情, 在C++裡面, 函數模板僅僅是一個用來生成函數的代碼塊而已,
* 他本身並沒有實體, 有就是沒有與 "未被實例化的那些程式" 相對應的程式代碼塊,
* 所以也就無法對其取地址( 因為就不存在啊, 怎麼會有地址?),
* 所以只有在用具體類型代替模板參數, 對該模板進行實例化以後才能有函數實體.
*
* 而這些函數指針要指向函數的入口地址, 那麼既然函數模板沒有具體的內存地址, 那麼指向函數模板的函數指針如何得到地址呢?
* 所以所謂的 " 函數模板指針 " 這個定義是無法通過以下的方法實現的:
* templatevoid (*sample)(T& );
*
* [二]
* 所以就要用以下的方法來實現, 讓模板類與模板函數再一起
*/
#include
using namespace std;
class CA {
public :
/*
由於我們在寫這些工具類的方法, 通常我們都是用類的靜態成員函數,
靜態成員函數的訪問和調用, 是通過ClassType::StaticMemberFunction來實現
*/
//static int Sum(int a, int b);
int Sum(int a, int b)
{
return a + b;
}
};
class CB {
public:
//static float Sum(float a, float b);
float Sum(float a, float b)
{
return a + b;
}
};
template
class CC {
public:
// function pointer type template
/*
會使用到typedef 是因為要讓邊義氣明白pClassFunc是一個函數指針類型名(不是函數指針),
這個類型的函數指針可以指向一個函數,
被指向的這個函數必須滿足以下條件: 返回值為 ParaType,
參數列表為 (ParaType, ParaType), 而且是一個定義在ClassType類中的成員函數.
注意: 需要再作用域標示符號後面, 函數指針類型名之前, 一定要加上*(dereference),
表明要定義的是函數指針類型.
如果不加上這個操作符號, 那這邊就會變成對一個名叫pClassFunc的函數成員進行泛化了,
那麼typedef關鍵字的存在似乎就沒有意義了
*/
typedef ParaType(ClassType::*pClassFunc)(ParaType, ParaType);
// function pointer method
ParaType Result(ClassType* pClassType, pClassFunc fun, ParaType a, ParaType b) {
/*
使用這個函數指真實, 需要創建一個類的實例, 對於函數指針的fun所指向的成員函數, 需要由實例來完成調用.
*/
return (pClassType->*fun)(a, b);
}
};
void main()
{
CA ca;
CC cc;
int a = 3;
int b = 4;
printf("A+B : %d \n", cc.Result(&ca, &CA::Sum, a, b));
CB cb;
CCfcc;
float fa = 3.3f;
float fb = 4.6f;
printf("A+B : %f \n", fcc.Result(&cb, &CB::Sum, fa, fb));
}
- Jul 30 Fri 2021 11:28
[教學]查詢檔案或資料夾被誰占用
- May 27 Thu 2021 17:43
[教學]C++ GetAdaptersAddresses(取得網卡和IP address)
這邊文章的重點只有三個,
第一是取得IP、第二是取得MAC、第三是解決symbol inet_ntop referenced in function的問題。
- May 12 Wed 2021 10:28
[教學- OpenCV] OpenCV 4.0.1 與 Visual Studio 2017 & Visual Studio 2019
- Sep 10 Thu 2020 17:40
[C++] Leetcode題( 1365. How Many Numbers Are Smaller Than the Current Number )
Given the array nums
, for each nums[i]
find out how many numbers in the array are smaller than it. That is, for each nums[i]
you have to count the number of valid j's
such that j != i
and nums[j] < nums[i]
.
Return the answer in an array.
- Sep 09 Wed 2020 10:50
[C++] Leetcode題( 1342. Number of Steps to Reduce a Number to Zero )
- Sep 09 Wed 2020 10:48
[C++] Leetcode題( 1528. Shuffle String )
- Sep 04 Fri 2020 11:12
[C++] Leetcode題( 771. Jewels and Stones )
You're given strings J
representing the types of stones that are jewels, and S
representing the stones you have. Each character in S
is a type of stone you have. You want to know how many of the stones you have are also jewels.
The letters in J
are guaranteed distinct, and all characters in J
and S
are letters. Letters are case sensitive, so "a"
is considered a different type of stone from "A"
.
- Sep 04 Fri 2020 11:08
[C++]C++ string中英文字母大小寫轉換
最近因為刷起leetcode,所以用到了蠻大量的string這個類型,
- Sep 01 Tue 2020 17:22
[C++] Leetcode題( 1512. Number of Good Pairs )
Given an array of integers nums
.
A pair (i,j)
is called good if nums[i]
== nums[j]
and i
< j
.
- Mar 17 Tue 2020 14:57
[教學]MFC Edit control transparent( editcontrol setTextcolor & setbkcolor )
- Mar 03 Tue 2020 09:26
[C++] UINT32 ( 依次輸出+2 進制的 print )
昨天在pa資料,突然想到這玩意其實也可以拿來分享,於是就有了這一篇文章。
- Feb 10 Mon 2020 12:15
[面試考題] C/C++
- Dec 31 Tue 2019 17:17
[教學]C++ 計算機概論- UINT32 ! 反轉吧!
- Dec 05 Thu 2019 18:43
[教學]C++/MFC - SetDisplayConfig
- Nov 21 Thu 2019 10:50
[教學]Using the Device Power API
Use the Device Power programming elements to manage the way devices perform while the system is in a sleep state.
Opening and closing the device list
Opening and closing the device list is a costly process in terms of CPU time. The DevicePowerOpen and DevicePowerClose functions that do this are only necessary if the application needs to query the device list multiple times.
- Nov 13 Wed 2019 15:06
[教學]MFC OnDeviceChange的用法(監聽裝置插拔)