C++ - 返回不同的变量类型
C++ - return different variable types
我不知道这是否可能,但也许还有其他解决方案可以满足我的需求。我正在尝试从设置文件中获取设置。它们可以是字符串(如名称(、整数或布尔值。当然,它们以文本形式存储在文件中,但我将创建一个用于打开和返回设置的类,但不是字符串,而是它们中的每一个实际上是什么。
class Settings {
public:
Settings(string FileName);
template <class T> T Setting(string SettingName);
}
例如,构造函数将加载文件,解析设置并将其存储为映射。现在,当我调用 Setting 成员函数时,我希望它识别所请求设置的值的类型(如果是数字,则为整数,如果为"真"或"假"布尔值,如果是字母数字为字符串(并返回该类型的值。一个例子
Settings UserPreferences("Preferences.cfg");
bool AutoLogin = UserPreferences.Setting("autologin"); // return bool
string UserName = UserPreferences.Setting("username"); // return string or char*
我查看了模板,但看起来我必须在创建 Settings 对象时指定我期望的变量,但这不是重点。我很高兴声明变量的类型要像这样返回:
bool AutoLogin = UserPreferences.Setting<bool>("autologin");
string UserName = UserPreferences.Setting<string>("username");
但我不知道这是否可能。你觉得怎么样?
这绝对是可能的,尽管您必须保证它可以转换为给定类型。这在XNA的ContentLoader中经常出现(尽管是一个很大的不同系统(。您可以使用此方法简化和抽象存储和检索内容的方式。考虑:
class Loader
{
private:
vector<void*> _items;
public:
template <typename Type>
Type GetItem( int index ) { return (Type)(_items[ index ]); }
};
这个想法是,只要你能可靠地(比示例更可靠(将内部数据强制转换为请求的类型,那么它就是一个完全合法的操作。如何保证成功完全是另一个问题,但您绝对可以拥有返回类型为其模板类型的方法。考虑以下示例(我将其用于资源加载器的大学项目(:
标题.h
class BasicResource
{
public:
static const int ResourceID;
const int ID;
BasicResource( )
: ID( ResourceID )
{
}
};
class Loader
{
private:
vector<BasicResource*> _items;
public:
template <typename Type>
Type GetItem( int index );
};
#include "inline.inl"
Inline.inl
template <typename Type>
Type Loader::GetItem( int index )
{
auto item = _items[ index ];
if( item != nullptr && item->ID == Type::ResourceID )
{
return (Type)_item;
}
else
{
// Handle the fail case somehow
}
}
内联文件允许您像往常一样分离逻辑,但将其包含在允许导出模板方法的标头中。
是的,这当然是可能的。我编写了以下完整的代码来证明这一点:
#include <iostream>
#include <map>
#include <string>
#include <sstream>
#include <stdexcept>
struct Settings
{
typedef std::map<std::string, std::string> SettingsMap;
template <class T> T as( const std::string& name ) const
{
std::istringstream is( getEntry( name ) );
T value;
if( is )
{
if( (is >> value) || (is.eof() && !is.fail()) )
{
return value;
}
}
//Exception handling not in scope of question
throw std::runtime_error( "..." );
};
const std::string& getEntry( const std::string& name ) const
{
SettingsMap::const_iterator pos( settingsMap_.find( name ) );
if( pos != settingsMap_.end() )
{
return pos->second;
}
//Not part of the scope of this answer....
throw std::invalid_argument( "No such setting..." );
}
Settings()
{
settingsMap_["mybool"] = "1";
settingsMap_["myint"] = "5";
settingsMap_["myfloat"] = "43.2";
}
SettingsMap settingsMap_;
};
int main()
{
Settings s;
std::cout << s.as<bool>("mybool") << " "
<< s.as<int>("myint") << " "
<< s.as<float>("myfloat");
return 0;
}
我已经实现了与此类似的内容,但是我使用了boost::any作为我的映射类型,并且在第一次解析期间读取了实际类型,因此确保存储的类型是正确的。我也使用了 boost::lexical_cast 而不是原生的 istringstream,但为了证明这一点,我省略了它。
- c++类声明时,相同的例程,不同的成员变量类型
- 如何为 c++ 的不同变量类型的结构元素创建动态数组?
- 创建类似于布尔值的变量类型
- std::cin 从控制台获取两种不同的变量类型,'storing'以后使用第二种类型?
- 如何使用变量模板比较 C++ 17 中的变量类型?
- 是否有一个库可以检查C++中的变量类型?
- 与不同变量类型相比,用于变量的 Clang AST 匹配器
- 继承类中的 C++ 成员变量类型重写
- 如何在编译器时动态获取元组内的变量类型C++?
- 在C++中动态分配变量类型
- 用于浮点值的最佳变量类型
- 定义变量类型
- 错误 - 变量类型 "X" 是一个抽象类 - c++
- 在函数 (c++) 中分配变量类型和大小数组
- 具有容器变量类型的类模板
- 获取变量类型名称
- 沿枚举 c++ 返回变量类型
- 是否可以通过模板指定抽象类变量类型
- 什么是变量类型位(32)
- C++ 在知道变量类型之前使用自动定义的变量