【发布时间】:2013-01-28 19:27:55
【问题描述】:
我正在尝试编写一个方法,它将派生自 std::string 的类作为参数。该方法重载了几个不同的函数签名。如果我尝试使用 std::string 调用它,或者至少是运行时错误,我希望编译失败,但显然编译器对我来说太聪明了。
class NotAString : public std::string {
NotAString(std::string str) : std::string(str) { }
};
class Foo {
Foo();
void bar(NotAString);
void bar(int)
};
这会编译并运行
Foo foo();
foo.bar(NotAString("baz"));
但这样做也是如此:
Foo foo();
foo.bar(std::string("baz"));
我尝试过像这样使用 typeid(str):
void Foo::Bar(NotAString str) {
if(typeid(&str) != typeid(new NotAString()) {
throw std::bad_typeid();
}
}
但如果将 std::string 或 NotAString 传递给它,它总是会抛出异常。我试过像这样使用 dynamic_cast:
void Foo::Bar(NotAString str) {
if (dynamic_cast<NotAString*>(&str) == NULL) {
throw std::bad_type();
}
}
但它从不抛出异常。
目标是能够区分字符串和表示键值查找的键的字符串。如何更改我的 NotAString 类或通过编译器强制执行一些更严格的类型检查,以使其按我的意愿工作?
【问题讨论】:
-
从
std::string派生是个坏主意。 -
NotAString是std::string。所以你有一个类,用它自己的名字,不是一个字符串,而是一个字符串。我要喝点东西。
标签: c++ overloading typechecking typeid