新聞中心
一、類模板特化
1.特化的實(shí)現(xiàn)
你可以為特定類型提供類模板的替代實(shí)現(xiàn)。例如,你可能認(rèn)為 const char 類型(C 風(fēng)格字符串)的 Grid 行為沒(méi)有意義。Grid

成都創(chuàng)新互聯(lián)一直在為企業(yè)提供服務(wù),多年的磨煉,使我們?cè)趧?chuàng)意設(shè)計(jì),成都全網(wǎng)營(yíng)銷到技術(shù)研發(fā)擁有了開發(fā)經(jīng)驗(yàn)。我們擅長(zhǎng)傾聽(tīng)企業(yè)需求,挖掘用戶對(duì)產(chǎn)品需求服務(wù)價(jià)值,為企業(yè)制作有用的創(chuàng)意設(shè)計(jì)體驗(yàn)。核心團(tuán)隊(duì)擁有超過(guò)十年以上行業(yè)經(jīng)驗(yàn),涵蓋創(chuàng)意,策化,開發(fā)等專業(yè)領(lǐng)域,公司涉及領(lǐng)域有基礎(chǔ)互聯(lián)網(wǎng)服務(wù)成都多線機(jī)房、手機(jī)APP定制開發(fā)、手機(jī)移動(dòng)建站、網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)絡(luò)整合營(yíng)銷。
模板的替代實(shí)現(xiàn)稱為模板特化。你可能會(huì)發(fā)現(xiàn)其語(yǔ)法初看有些奇怪。當(dāng)你編寫類模板特化時(shí),你必須指定這是模板,并且你正在為特定類型編寫模板的版本。以下是 Grid 的 const char
export module grid:string;
// 當(dāng)使用模板特化時(shí),原始模板也必須可見(jiàn)。
import :main;
export template <>
class Grid {
public:
explicit Grid(size_t width = DefaultWidth, size_t height = DefaultHeight);
virtual ~Grid() = default;
// 明確默認(rèn)拷貝構(gòu)造函數(shù)和賦值運(yùn)算符。
Grid(const Grid& src) = default;
Grid& operator=(const Grid& rhs) = default;
// 明確默認(rèn)移動(dòng)構(gòu)造函數(shù)和賦值運(yùn)算符。
Grid(Grid&& src) = default;
Grid& operator=(Grid&& rhs) = default;
std::optional& at(size_t x, size_t y);
const std::optional& at(size_t x, size_t y) const;
size_t getHeight() const { return m_height; }
size_t getWidth() const { return m_width; }
static const size_t DefaultWidth { 10 };
static const size_t DefaultHeight { 10 };
private:
void verifyCoordinate(size_t x, size_t y) const;
std::vector>> m_cells;
size_t m_width { 0 }, m_height { 0 };
}; 注意,在特化中你不使用任何類型變量,例如 T,你直接使用 const char* 和字符串。此時(shí)一個(gè)明顯的問(wèn)題是,為什么這個(gè)類仍然是模板。即,以下語(yǔ)法有什么用途?
template <> class Grid這種語(yǔ)法告訴編譯器,這個(gè)類是 Grid 類的 const char* 特化。假設(shè)你沒(méi)有使用這種語(yǔ)法,而是嘗試編寫如下代碼:
class Grid編譯器不會(huì)允許你這樣做,因?yàn)橐呀?jīng)存在一個(gè)名為 Grid 的類模板(原始類模板)。只有通過(guò)特化,你才能重用這個(gè)名稱。特化的主要好處是它們對(duì)用戶來(lái)說(shuō)可以是不可見(jiàn)的。當(dāng)用戶創(chuàng)建 int 或 SpreadsheetCells 的 Grid 時(shí),編譯器會(huì)從原始 Grid 模板生成代碼。當(dāng)用戶創(chuàng)建 const char* 的 Grid 時(shí),編譯器使用 const char* 特化。這一切都可以在“幕后”進(jìn)行。
2.主模塊接口文件
主模塊接口文件簡(jiǎn)單地導(dǎo)入并導(dǎo)出兩個(gè)模塊接口分區(qū):
export module grid;
export import :main;
export import :string;特化可以按照以下方式進(jìn)行測(cè)試:
Grid myIntGrid; // 使用原始 Grid 模板。
Grid stringGrid1 { 2, 2 }; // 使用 const char* 特化。
const char* dummy { "dummy" };
stringGrid1.at(0, 0) = "hello";
stringGrid1.at(0, 1) = dummy;
stringGrid1.at(1, 0) = dummy;
stringGrid1.at(1, 1) = "there";
Grid stringGrid2 { stringGrid1 }; 當(dāng)你特化一個(gè)模板時(shí),你不會(huì)“繼承”任何代碼;特化不像派生。你必須重寫類的整個(gè)實(shí)現(xiàn)。沒(méi)有要求你提供具有相同名稱或行為的方法。例如,const char* 的 Grid 特化實(shí)現(xiàn)了 at() 方法,返回 optional
下面是 const char* 特化的方法實(shí)現(xiàn)。與模板定義中不同,你不需要在每個(gè)方法定義前重復(fù) template<> 語(yǔ)法。
Grid::Grid(size_t width, size_t height)
: m_width { width }, m_height { height } {
m_cells.resize(m_width);
for (auto& column : m_cells) {
column.resize(m_height);
}
}
void Grid::verifyCoordinate(size_t x, size_t y) const {
if (x >= m_width) {
throw std::out_of_range { std::format("{} must be less than {}.", x, m_width) };
}
if (y >= m_height) {
throw std::out_of_range { std::format("{} must be less than {}.", y, m_height) };
}
}
const std::optional& Grid::at(size_t x, size_t y) const {
verifyCoordinate(x, y);
return m_cells[x][y];
}
std::optional& Grid::at(size_t x, size_t y) {
return const_cast&>(std::as_const(*this).at(x, y));
} 二、從類模板派生
派生自類模板
您可以從類模板繼承。如果派生類從模板本身繼承,它也必須是一個(gè)模板。另外,您可以從類模板的特定實(shí)例繼承,在這種情況下,您的派生類不需要是一個(gè)模板。
作為前者的一個(gè)例子,假設(shè)您決定通用的 Grid 類沒(méi)有提供足夠的功能來(lái)用作游戲棋盤。具體來(lái)說(shuō),您希望為游戲棋盤添加一個(gè) move() 方法,將棋子從棋盤上的一個(gè)位置移動(dòng)到另一個(gè)位置。以下是 GameBoard 模板的類定義:
import grid;
export template
class GameBoard : public Grid {
public:
explicit GameBoard(size_t width = Grid::DefaultWidth, size_t height = Grid::DefaultHeight);
void move(size_t xSrc, size_t ySrc, size_t xDest, size_t yDest);
}; 這個(gè) GameBoard 模板派生自 Grid 模板,從而繼承了所有其功能。您不需要重寫 at()、getHeight() 或任何其他方法。您也不需要添加拷貝構(gòu)造函數(shù)、operator= 或析構(gòu)函數(shù),因?yàn)槟?GameBoard 中沒(méi)有任何動(dòng)態(tài)分配的內(nèi)存。繼承語(yǔ)法看起來(lái)很正常,除了基類是 Grid
例如,如果您使用 ChessPiece 類型實(shí)例化一個(gè) GameBoard,那么編譯器也會(huì)為 Grid
template
GameBoard::GameBoard(size_t width, size_t height) : Grid { width, height } { }
template
void GameBoard::move(size_t xSrc, size_t ySrc, size_t xDest, size_t yDest) {
Grid::at(xDest, yDest) = std::move(Grid::at(xSrc, ySrc));
Grid::at(xSrc, ySrc).reset(); // 重置源單元
// 或者:
// this->at(xDest, yDest) = std::move(this->at(xSrc, ySrc));
// this->at(xSrc, ySrc).reset();
} 您可以按以下方式使用 GameBoard 模板:
GameBoard chessboard { 8, 8 };
ChessPiece pawn;
chessBoard.at(0, 0) = pawn;
chessBoard.move(0, 0, 0, 1); 注意:當(dāng)然,如果您想重寫 Grid 中的方法,您必須在 Grid 類模板中將它們標(biāo)記為虛擬的。
三、繼承與特化
|
特性 |
繼承 |
特化 |
|
代碼復(fù)用? |
是:派生類包含所有基類的數(shù)據(jù)成員和方法。 |
否:您必須在特化中重寫所有所需代碼。 |
|
名稱復(fù)用? |
否:派生類名稱必須與基類名稱不同。 |
是:特化必須與原始模板具有相同的名稱。 |
|
支持多態(tài)性? |
是:派生類的對(duì)象可以代替基類的對(duì)象。 |
否:每個(gè)類型的模板實(shí)例化都是不同的類型。 |
注意:使用繼承來(lái)擴(kuò)展實(shí)現(xiàn)和實(shí)現(xiàn)多態(tài)性。使用特化來(lái)為特定類型定制實(shí)現(xiàn)。
網(wǎng)頁(yè)名稱:C++類模板特化與繼承使用說(shuō)明書,新手也能get
瀏覽地址:http://www.dlmjj.cn/article/dhgihoo.html


咨詢
建站咨詢
