在c++中思考时遇到的友元迭代器和友元类迭代器有什么区别?
《Thinking in C++》第 1 卷第 16 章:模板简介。 背景:
请注意,不要只是说:
friend iterator; // Make it a friend
这段代码有:
friend class iterator; // Make it a friend
这很重要,因为名称“iterator”已经在包含文件的范围内。
埃克尔上面的真正意思是什么? 看来 friend iterator
编译正确,我看不到差异。谁能告诉答案吗?谢谢
In Thinking in C++ Volume 1, chapter 16: Introduction to Templates.
The context:
Notice that instead of just saying:
friend iterator; // Make it a friend
This code has:
friend class iterator; // Make it a friend
This is important because the name "iterator" is already in scope, from an included file.
What does Eckel really mean above?
It seems friend iterator
compiles correctly and I can't see the differences. Can anyone tell the answer? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 C++03 标准第 11.4 节:
应在类的友元声明中使用详细类型说明符。
因此,根据规范,编译器将发出警告您认为迭代器的友元声明必须是详细的类名。如果不是,那么编译者在这个特定方面不符合标准。
什么是详细类型说明符?
C++ 使用详细的类型说明符来明确告诉编译器将类视为类。我认为 MSDN 可以比我更好地解释它,所以检查 this 以获得详细解释。
As per C++03 standard section 11.4:
An elaborated-type-specifier shall be used in a friend declaration for a class.
So as per the specification the compiler will warn you that the friend declaration of
iterator
must be an elaborated class name. If not then the complier is non-compliant to the standard in this particular aspect.What are Elaborated Type Specifiers?
C++ use elaborated type specifiers to tell the compiler explicitly to treat a class as a class. I think MSDN can explain it much better than I can, So check this out for detailed explanation.