在下面的C++代码中,有人能解释一下private部分中的每一行是什么意思吗?我试着查过了,但我还是搞不懂他们是做什么的。
我知道using等同于C中的typedef,所以:
using the_graph = graph<T_node, T_edge1, T_allocator, T_size>;意味着您使用the_graph。
但是,在这种情况下,为什么要对它调用作用域解析操作符呢?
我不认为它是4种described here方法中的任何一种。
template <class T_node, class T_edge1, class T_edge2, class T_allocator, class T_size = uint32_t>
class graph : private virtual graph<T_node, T_edge1, T_allocator, T_size>, private virtual graph<T_node, T_edge2, T_allocator, T_size>
{
public:
using the_graph = graph<T_node, T_edge1, T_allocator, T_size>;
private:
using typename the_graph::node_list_iterator;
using the_graph::node_begin;
};发布于 2018-08-17 02:50:05
using指令用于将不属于当前作用域的名称带入当前作用域。
示例:
struct Foo
{
struct Bar {};
};
int main()
{
Bar b1; // Not ok. Bar is not in scope yet.
using Foo::Bar;
Bar b2; // Ok. Bar is now in scope.
}当名称依赖于模板参数时,标准要求您使用精心设计的形式
using typename the_graph::node_list_iterator;在该行之后,您可以使用node_list_iterator作为类型名。
如果the_graph不是一个类模板,您可以使用更简单的形式
using the_graph::node_list_iterator;进一步阅读:Where and why do I have to put the "template" and "typename" keywords?
发布于 2018-08-17 02:44:16
这些using使关联的名称可见,并避免歧义。
两个库都应该具有类型node_list_iterator和方法node_begin。
发布于 2018-08-17 02:48:55
在模板的定义和声明中,关键字typename用于指定限定id(即some_name::some_member)是类型而不是变量。这个关键字在模板实例化之前是必需的,因为编译器不知道some_name的定义,如果没有typename,它会假设some_member是一个变量(或函数)。
然后,using指令using typename::node_list_iterator使node_list_iterator可以从graph类私有访问。
https://stackoverflow.com/questions/51883423
复制相似问题