日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

C++類模板特化與繼承使用說明書,新手也能get

來源: 責編: 時間:2023-12-20 17:46:21 235觀看
導讀一、類模板特化1.特化的實現(xiàn)你可以為特定類型提供類模板的替代實現(xiàn)。例如,你可能認為 const char 類型(C 風格字符串)的 Grid 行為沒有意義。Grid<const char> 將在 vector<vector<optional<const char*>>> 中存儲其元素

一、類模板特化

1.特化的實現(xiàn)

你可以為特定類型提供類模板的替代實現(xiàn)。例如,你可能認為 const char 類型(C 風格字符串)的 Grid 行為沒有意義。Grid<const char> 將在 vector<vector<optional<const char*>>> 中存儲其元素。拷貝構造函數(shù)和賦值運算符將執(zhí)行這些 const char 指針類型的淺拷貝。對于 const char,進行深拷貝字符串可能更有意義。最簡單的解決方案是為 const char 編寫一個專門的實現(xiàn),將它們轉換為 C++ 字符串,并存儲在 vector<vector<optional<string>>> 中。gT828資訊網(wǎng)——每日最新資訊28at.com

gT828資訊網(wǎng)——每日最新資訊28at.com

模板的替代實現(xiàn)稱為模板特化。你可能會發(fā)現(xiàn)其語法初看有些奇怪。當你編寫類模板特化時,你必須指定這是模板,并且你正在為特定類型編寫模板的版本。以下是 Grid 的 const chargT828資訊網(wǎng)——每日最新資訊28at.com

export module grid:string;// 當使用模板特化時,原始模板也必須可見。import :main;export template <>class Grid<const char*> {public:    explicit Grid(size_t width = DefaultWidth, size_t height = DefaultHeight);    virtual ~Grid() = default;    // 明確默認拷貝構造函數(shù)和賦值運算符。    Grid(const Grid& src) = default;    Grid& operator=(const Grid& rhs) = default;    // 明確默認移動構造函數(shù)和賦值運算符。    Grid(Grid&& src) = default;    Grid& operator=(Grid&& rhs) = default;    std::optional<std::string>& at(size_t x, size_t y);    const std::optional<std::string>& 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<std::vector<std::optional<std::string>>> m_cells;    size_t m_width { 0 }, m_height { 0 };};

注意,在特化中你不使用任何類型變量,例如 T,你直接使用 const char* 和字符串。此時一個明顯的問題是,為什么這個類仍然是模板。即,以下語法有什么用途?gT828資訊網(wǎng)——每日最新資訊28at.com

template <> class Grid<const char*>

這種語法告訴編譯器,這個類是 Grid 類的 const char* 特化。假設你沒有使用這種語法,而是嘗試編寫如下代碼:gT828資訊網(wǎng)——每日最新資訊28at.com

class Grid

編譯器不會允許你這樣做,因為已經(jīng)存在一個名為 Grid 的類模板(原始類模板)。只有通過特化,你才能重用這個名稱。特化的主要好處是它們對用戶來說可以是不可見的。當用戶創(chuàng)建 int 或 SpreadsheetCells 的 Grid 時,編譯器會從原始 Grid 模板生成代碼。當用戶創(chuàng)建 const char* 的 Grid 時,編譯器使用 const char* 特化。這一切都可以在“幕后”進行。gT828資訊網(wǎng)——每日最新資訊28at.com

2.主模塊接口文件

主模塊接口文件簡單地導入并導出兩個模塊接口分區(qū):gT828資訊網(wǎng)——每日最新資訊28at.com

export module grid;export import :main;export import :string;

特化可以按照以下方式進行測試:gT828資訊網(wǎng)——每日最新資訊28at.com

Grid<int> myIntGrid; // 使用原始 Grid 模板。Grid<const char*> 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<const char*> stringGrid2 { stringGrid1 };

當你特化一個模板時,你不會“繼承”任何代碼;特化不像派生。你必須重寫類的整個實現(xiàn)。沒有要求你提供具有相同名稱或行為的方法。例如,const char* 的 Grid 特化實現(xiàn)了 at() 方法,返回 optional<string>,而不是 optional<const char*>。事實上,你可以編寫一個與原始類完全不相關的完全不同的類。當然,這會濫用模板特化功能,如果沒有充分理由,你不應該這樣做。gT828資訊網(wǎng)——每日最新資訊28at.com

下面是 const char* 特化的方法實現(xiàn)。與模板定義中不同,你不需要在每個方法定義前重復 template<> 語法。gT828資訊網(wǎng)——每日最新資訊28at.com

Grid<const char*>::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<const char*>::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<std::string>& Grid<const char*>::at(size_t x, size_t y) const {    verifyCoordinate(x, y);    return m_cells[x][y];}std::optional<std::string>& Grid<const char*>::at(size_t x, size_t y) {    return const_cast<std::optional<std::string>&>(std::as_const(*this).at(x, y));}

二、從類模板派生

派生自類模板

您可以從類模板繼承。如果派生類從模板本身繼承,它也必須是一個模板。另外,您可以從類模板的特定實例繼承,在這種情況下,您的派生類不需要是一個模板。gT828資訊網(wǎng)——每日最新資訊28at.com

作為前者的一個例子,假設您決定通用的 Grid 類沒有提供足夠的功能來用作游戲棋盤。具體來說,您希望為游戲棋盤添加一個 move() 方法,將棋子從棋盤上的一個位置移動到另一個位置。以下是 GameBoard 模板的類定義:gT828資訊網(wǎng)——每日最新資訊28at.com

import grid;export template <typename T>class GameBoard : public Grid<T> {public:    explicit GameBoard(size_t width = Grid<T>::DefaultWidth, size_t height = Grid<T>::DefaultHeight);    void move(size_t xSrc, size_t ySrc, size_t xDest, size_t yDest);};

這個 GameBoard 模板派生自 Grid 模板,從而繼承了所有其功能。您不需要重寫 at()、getHeight() 或任何其他方法。您也不需要添加拷貝構造函數(shù)、operator= 或析構函數(shù),因為您在 GameBoard 中沒有任何動態(tài)分配的內(nèi)存。繼承語法看起來很正常,除了基類是 Grid<T>,而不是 Grid。這種語法的原因是 GameBoard 模板并不真正從通用的 Grid 模板派生。相反,GameBoard 模板的每個實例化都派生自相同類型的 Grid 實例化。gT828資訊網(wǎng)——每日最新資訊28at.com

例如,如果您使用 ChessPiece 類型實例化一個 GameBoard,那么編譯器也會為 Grid<ChessPiece> 生成代碼。: public Grid<T> 語法表示這個類繼承自對于 T 類型參數(shù)有意義的任何 Grid 實例化。請注意,盡管一些編譯器不強制執(zhí)行,但 C++ 名稱查找規(guī)則要求您使用 this 指針或 Grid<T>:: 來引用基類模板中的數(shù)據(jù)成員和方法。因此,我們使用 Grid<T>::DefaultWidth 而不是僅僅使用 DefaultWidth。以下是構造函數(shù)和 move() 方法的實現(xiàn):gT828資訊網(wǎng)——每日最新資訊28at.com

template <typename T>GameBoard<T>::GameBoard(size_t width, size_t height) : Grid<T> { width, height } { }template <typename T>void GameBoard<T>::move(size_t xSrc, size_t ySrc, size_t xDest, size_t yDest) {    Grid<T>::at(xDest, yDest) = std::move(Grid<T>::at(xSrc, ySrc));    Grid<T>::at(xSrc, ySrc).reset(); // 重置源單元    // 或者:    // this->at(xDest, yDest) = std::move(this->at(xSrc, ySrc));    // this->at(xSrc, ySrc).reset();}

您可以按以下方式使用 GameBoard 模板:gT828資訊網(wǎng)——每日最新資訊28at.com

GameBoard<ChessPiece> chessboard { 8, 8 };ChessPiece pawn;chessBoard.at(0, 0) = pawn;chessBoard.move(0, 0, 0, 1);

注意:當然,如果您想重寫 Grid 中的方法,您必須在 Grid 類模板中將它們標記為虛擬的。gT828資訊網(wǎng)——每日最新資訊28at.com

gT828資訊網(wǎng)——每日最新資訊28at.com

三、繼承與特化

特性
gT828資訊網(wǎng)——每日最新資訊28at.com

繼承
gT828資訊網(wǎng)——每日最新資訊28at.com

特化
gT828資訊網(wǎng)——每日最新資訊28at.com

代碼復用?
gT828資訊網(wǎng)——每日最新資訊28at.com

是:派生類包含所有基類的數(shù)據(jù)成員和方法。
gT828資訊網(wǎng)——每日最新資訊28at.com

否:您必須在特化中重寫所有所需代碼。
gT828資訊網(wǎng)——每日最新資訊28at.com

名稱復用?
gT828資訊網(wǎng)——每日最新資訊28at.com

否:派生類名稱必須與基類名稱不同。
gT828資訊網(wǎng)——每日最新資訊28at.com

是:特化必須與原始模板具有相同的名稱。
gT828資訊網(wǎng)——每日最新資訊28at.com

支持多態(tài)性?
gT828資訊網(wǎng)——每日最新資訊28at.com

是:派生類的對象可以代替基類的對象。
gT828資訊網(wǎng)——每日最新資訊28at.com

否:每個類型的模板實例化都是不同的類型。
gT828資訊網(wǎng)——每日最新資訊28at.com

注意:使用繼承來擴展實現(xiàn)和實現(xiàn)多態(tài)性。使用特化來為特定類型定制實現(xiàn)。gT828資訊網(wǎng)——每日最新資訊28at.com

gT828資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-50748-0.htmlC++類模板特化與繼承使用說明書,新手也能get

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 微服務集成中的三個常見缺陷,以及如何避免它們

下一篇: 尤雨溪:Vue 3 開發(fā)中的經(jīng)驗和教訓

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 开鲁县| 宜丰县| 清丰县| 阳信县| 海南省| 南宫市| 东光县| 尚义县| 石楼县| 温泉县| 弥渡县| 遵化市| 田林县| 方正县| 龙川县| 馆陶县| 邮箱| 南宫市| 穆棱市| 淮滨县| 武定县| 西贡区| 绥化市| 尼木县| 察雅县| 于田县| 黑山县| 灵璧县| 辉县市| 大田县| 青阳县| 晋州市| 乐山市| 清河县| 满洲里市| 甘孜| 仁布县| 许昌市| 迭部县| 彝良县| 慈溪市|