c – 我无法理解结构如何替换typedef
发布时间:2020-12-16 10:04:45 所属栏目:百科 来源:网络整理
导读:看到这个链接: https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1l=EN-USk=k(C4503)rd=true它建议写: // C4503b.cpp // compile with: /W1 /EHsc /c #include string #include map class Field{}; struct Screen2 { std::mapstd::string,Fie
看到这个链接:
https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k(C4503)&rd=true它建议写:
// C4503b.cpp // compile with: /W1 /EHsc /c #include <string> #include <map> class Field{}; struct Screen2 { std::map<std::string,Field> Element; }; struct WebApp2 { std::map<std::string,Screen2> Element; }; struct WebAppTest2 { std::map<std::string,WebApp2> Element; }; struct Hello2 { std::map<std::string,WebAppTest2> Element; }; Hello2 MyWAT2; 代替 // C4503.cpp // compile with: /W1 /EHsc /c // C4503 expected #include <string> #include <map> class Field{}; typedef std::map<std::string,Field> Screen; typedef std::map<std::string,Screen> WebApp; typedef std::map<std::string,WebApp> WebAppTest; typedef std::map<std::string,WebAppTest> Hello; Hello MyWAT; 但是这些代码与typedef不等同.Hello是一个std :: map,而对于struct,它只是一个结构,它有一个字段,这是一个映射,这意味着我不能互换使用它们.有人能解释一下吗? 解决方法
你是对的,你不能互换使用它们.实际上,Microsoft建议您这样做是为了克服技术上的困难:他们不能(或有困难)处理超过4096字节的错位名称.
在大多数情况下,我相信typedef会比聚合更好. 但是由于你的编译器有些限制,你可能会陷入他们的黑客行为. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |