public class BaseRequest : IRequest
{
public string Command { get; } = "";
}
public class GetRequest : IRequest
{
public string Command => "get";
public string Key { get; set; } = string.Empty;
}
public class SetRequest : IRequest
{
public string Command => "set";
public string Key { get; set; } = string.Empty;
public string Value { get; set; } = string.Empty;
}
Run Code Online (Sandbox Code Playgroud)
现在这是一个 json 数据字符串,我需要将其转换为基类以确定它有哪个命令。
public class BaseRequest : IRequest
{
public string Command { get; …Run Code Online (Sandbox Code Playgroud) reinterpret_cast使用转换std::list<Derived *>为安全吗std::list<Base *>?
class Base {
...
}
class Derived : public Base{
...
}
void func(const std::list<Base *> &list);
int main() {
std::list<Derived *> list1;
// ...
func(*reinterpret_cast<std::list<Base *> *>(&list1)); // Safe ?
}
Run Code Online (Sandbox Code Playgroud)
如果是安全的转换,我们不需要复制列表,这可能会提高性能。