vc中typedef的使用
来源:百度知道 编辑:UC知道 时间:2024/05/21 07:43:35
#include<iostream.h>
void main()
{
typedef enum mei;
enum mycolor{father,mother,brother,sister=9};
mycolor a=sister;
cout<<a<<endl;
}
写为mei mycolor{father,mother,brother,sister=9};
就报错,不知道为什么?
莫非是typedef不能对enum类型起作用?请指教
void main()
{
typedef enum mei;
enum mycolor{father,mother,brother,sister=9};
mycolor a=sister;
cout<<a<<endl;
}
写为mei mycolor{father,mother,brother,sister=9};
就报错,不知道为什么?
莫非是typedef不能对enum类型起作用?请指教
enum 和 struct、class差不多, 是一个类型的修饰词, 而不是特定的类, typedef之后跟的必须是一个特定的类型. 应该这样:
#include <iostream>
using namespace std;
int main()
{
enum MyColor{red, blue, green};
typedef MyColor C;
C a = blue;
cout << a;
}
如果你要把enum换成mei就只能用#define了:
#define mei enum
mei MyColor{red, blue, ...};
还有楼上的, 你这句:
"其实typedef在C++里边没多少用处。 ", 有用没有你看看下面这段代码就知道了, 不过估计你也看不懂:
#include <iostream>
#include <typeinfo>
using namespace std;
template <int n, class T>
struct Test1
{
typedef typename const Test1<n-1, T>::type* type;
};
template <class T>
struct Test1<0, T>
{
typedef T* type;
};
template <int n, class T>
struct Test2
{
typedef
pair<pair<typename Test2<n-1, T>::type, T >, pair<