C++如何在一個(gè)函數(shù)內(nèi)返回不同類型(三種方法)
C++ 中要在一個(gè)函數(shù)內(nèi)返回不同類型的值,你可以使用 C++17 引入的 std::variant 或 std::any,或者使用模板和多態(tài)。下面將分別介紹這些方法。
方法一:使用 std::variant
std::variant 允許你在一個(gè)函數(shù)內(nèi)返回不同類型的值,但它要求所有可能的返回類型都在一個(gè)有限的集合中,你需要提前定義這個(gè)集合。
首先,包括 <variant> 頭文件:
#include <variant>
然后,使用 std::variant 來定義函數(shù)的返回類型:
std::variant<int, double, std::string> GetDifferentValue(int choice) {
if (choice == 0) {
return 42;
} else if (choice == 1) {
return 3.14;
} else {
return "Hello, World!";
}
}在這個(gè)示例中,GetDifferentValue 函數(shù)可以返回 int、double 或 std::string,具體返回哪種類型取決于 choice 參數(shù)的值。
方法二:使用 std::any
std::any 允許你在一個(gè)函數(shù)內(nèi)返回不同類型的值,而無需提前定義可能的返回類型。但在使用 std::any 時(shí),你需要小心類型安全和類型轉(zhuǎn)換。
首先,包括 <any> 頭文件:
#include <any>
然后,使用 std::any 來定義函數(shù)的返回類型:
std::any GetDifferentValue(int choice) {
if (choice == 0) {
return 42;
} else if (choice == 1) {
return 3.14;
} else {
return "Hello, World!";
}
}在這個(gè)示例中,GetDifferentValue 函數(shù)可以返回任何類型的值。
方法三:使用模板和多態(tài)
另一種方式是使用模板和多態(tài),這樣你可以在運(yùn)行時(shí)動態(tài)確定返回的類型。這通常需要?jiǎng)?chuàng)建一個(gè)基類,派生出具體類型的子類,并使用基類指針或智能指針進(jìn)行返回。
#include <iostream>
#include <memory>
class Base {
public:
virtual void print() const = 0;
};
class IntType : public Base {
public:
IntType(int value) : value(value) {}
void print() const override {
std::cout << "Int: " << value << std::endl;
}
private:
int value;
};
class DoubleType : public Base {
public:
DoubleType(double value) : value(value) {}
void print() const override {
std::cout << "Double: " << value << std::endl;
}
private:
double value;
};
class StringType : public Base {
public:
StringType(const std::string& value) : value(value) {}
void print() const override {
std::cout << "String: " << value << std::endl;
}
private:
std::string value;
};
std::unique_ptr<Base> GetDifferentValue(int choice) {
if (choice == 0) {
return std::make_unique<IntType>(42);
} else if (choice == 1) {
return std::make_unique<DoubleType>(3.14);
} else {
return std::make_unique<StringType>("Hello, World!");
}
}
int main() {
auto value = GetDifferentValue(2);
value->print();
return 0;
}在這個(gè)示例中,GetDifferentValue 返回一個(gè)指向 Base 基類的智能指針,而 Base 有多個(gè)派生類,代表不同的返回類型。
以上是三種在 C++ 中返回不同類型的方法,你可以根據(jù)具體需求選擇其中之一。
到此這篇關(guān)于你知道C++如何在一個(gè)函數(shù)內(nèi)返回不同類型嗎?的文章就介紹到這了,更多相關(guān)C++一個(gè)函數(shù)內(nèi)返回不同類型內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++將二叉樹轉(zhuǎn)為雙向鏈表及判斷兩個(gè)鏈表是否相交
這篇文章主要介紹了C++將二叉樹轉(zhuǎn)為雙向鏈表及判斷兩個(gè)鏈表是否相交的方法,文中還給出了求兩個(gè)鏈表相交的第一個(gè)節(jié)點(diǎn)列的實(shí)現(xiàn)方法,需要的朋友可以參考下2016-02-02
一文帶你了解C語言中的動態(tài)內(nèi)存管理函數(shù)
C語言中內(nèi)存管理相關(guān)的函數(shù)主要有realloc、calloc、malloc、free等,這篇文章主要為大家講解一下這四個(gè)函數(shù)的具體用法,需要的可以參考一下2023-03-03
關(guān)于C++STL string類的介紹及模擬實(shí)現(xiàn)
這篇文章主要介紹了關(guān)于C++STL string類的介紹及模擬實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下面具體的文章內(nèi)容2021-09-09
簡單掌握C++編程中的while與do-while循環(huán)語句使用
這篇文章主要介紹了C++編程中的while與do-while循環(huán)語句使用,區(qū)別就是while是先判斷再執(zhí)行,而do-while是先執(zhí)行再判斷,需要的朋友可以參考下2016-01-01
C++中為何推薦要把基類析構(gòu)函數(shù)設(shè)置成虛函數(shù)
這篇文章主要介紹了C++中為何推薦要把基類析構(gòu)函數(shù)設(shè)置成虛函數(shù)問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12

