C ++ - 组合typedef和typename的语句的含义 [英] C++ - meaning of a statement combining typedef and typename

查看:333
本文介绍了C ++ - 组合typedef和typename的语句的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++头文件中,我看到了这个代码:

In a C++ header file, I am seeing this code:

typedef typename _Mybase::value_type value_type;

现在,根据我的理解,引用作者:Schildt。 typename 可以用关键字类替换,第二次使用 typename 是通知编译器在模板声明是一个类型名称而不是对象名称。

Now, as I understand, quoting from « C++ the Complete Reference » by Schildt. typename can be substituted by keyword class, the second use of typename is to inform the compiler that a name used in a template declaration is a type name rather than an object name.

同样,您可以使用关键字 typedef 定义新的数据类型名称。你不是
实际创建一个新的数据类型,而是为一个现有的
类型定义一个新的名称。

Similarly, you can define new data type names by using the keyword typedef. You are not actually creating a new data type, but rather defining a new name for an existing type.

但是,你可以解释一下上面代码行的含义,其中 typedef typename 组合在一起。在语句中的 :: 是什么意思?

However, can you explain exactly what is the meaning of the above line of code, where typedef and typename are combined together. And what does the "::" in the statement imply?

推荐答案

typedef正在定义一个新的类型,在你的代码中使用,就像一个缩写。

typedef is defining a new type for use in your code, like a shorthand.

typedef typename _MyBase::value_type value_type;
value_type v;
//use v

这里的typename是让编译器知道 value_type _MyBase 中的类型而不是对象。

typename here is letting the compiler know that value_type is a type and not an object inside of _MyBase.

code> :: 是类型的范围。它是一种像is in,因此 value_type _MyBase 中。或者也可以被认为包含。

the :: is the scope of the type. It is kind of like "is in" so value_type "is in" _MyBase. or can also be thought of as contains.

这篇关于C ++ - 组合typedef和typename的语句的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆