关于typedef 在类中使用的作用域, 继承以及重载。
2013-03-28 16:05
453 查看
1、typedef也是有作用域的,作用域和函数、变量类似。比如类内定义的typedef,要在类外引用就必须在前面加上class_name:: 。并且受到private、public的影响。
2、typedef也具有继承性,比如父类里面定义了typedef,子类里是可以使用的。比如:
class a
{
public:
typedef int typea;
}
class b : public a
{
public:
}
这时候可以使用 a::typea, 也可以使用b::typea, 结果是一样的。
3、typedef同样可以重载,就像函数重载那样。比如:
class c : public a
{
public:
typedef char typea;
}
这种时候typea就被重载了, 这种情况下c类的对象,在调用函数时候c自己的成员函数调用是char、父类a的成员函数调用结果是int.不存在虚继承的情况。
测试代码如下:
class a
{
public:
typedef int typeA;// 4bytes
public:
void testa()
{
printf("class a. typeA size is : %d\n", sizeof(typeA));
}
};
class b : public a
{
public:
void testb()
{
printf("class b. typeA size is : %d\n", sizeof(typeA));
}
};
class c : public a
{
public:
void testc1()
{
printf("class c. before redef. typeA size is : %d\n", sizeof(typeA));
}
typedef char typeA;// 1byte.
void testc2()
{
printf("class c. after redef. typeA size is : %d\n", sizeof(typeA));
}
};
int _tmain(int, _TCHAR*)
{
b objb;
c objc;
printf("Start object with class b test...\n");
objb.testa();
objb.testb();
printf("\nStart object with class c test...\n");
objc.testa();
objc.testc1();
objc.testc2();
printf("\n");
printf("c::typeA size = %d, b::typeA size = %d, a::typeA size = %d\n", sizeof(c::typeA), sizeof(b::typeA), sizeof(a::typeA));
system("pause");
return 0;
}
// 结果:
Start object with class b test...
class a. typeA size is : 4
class b. typeA size is : 4
Start object with class c test...
class a. typeA size is : 4
class c. before redef. typeA size is : 1
class c. after redef. typeA size is : 1
c::typeA size = 1, b::typeA size = 4, a::typeA size = 4
请按任意键继续. . .
2、typedef也具有继承性,比如父类里面定义了typedef,子类里是可以使用的。比如:
class a
{
public:
typedef int typea;
}
class b : public a
{
public:
}
这时候可以使用 a::typea, 也可以使用b::typea, 结果是一样的。
3、typedef同样可以重载,就像函数重载那样。比如:
class c : public a
{
public:
typedef char typea;
}
这种时候typea就被重载了, 这种情况下c类的对象,在调用函数时候c自己的成员函数调用是char、父类a的成员函数调用结果是int.不存在虚继承的情况。
测试代码如下:
class a
{
public:
typedef int typeA;// 4bytes
public:
void testa()
{
printf("class a. typeA size is : %d\n", sizeof(typeA));
}
};
class b : public a
{
public:
void testb()
{
printf("class b. typeA size is : %d\n", sizeof(typeA));
}
};
class c : public a
{
public:
void testc1()
{
printf("class c. before redef. typeA size is : %d\n", sizeof(typeA));
}
typedef char typeA;// 1byte.
void testc2()
{
printf("class c. after redef. typeA size is : %d\n", sizeof(typeA));
}
};
int _tmain(int, _TCHAR*)
{
b objb;
c objc;
printf("Start object with class b test...\n");
objb.testa();
objb.testb();
printf("\nStart object with class c test...\n");
objc.testa();
objc.testc1();
objc.testc2();
printf("\n");
printf("c::typeA size = %d, b::typeA size = %d, a::typeA size = %d\n", sizeof(c::typeA), sizeof(b::typeA), sizeof(a::typeA));
system("pause");
return 0;
}
// 结果:
Start object with class b test...
class a. typeA size is : 4
class b. typeA size is : 4
Start object with class c test...
class a. typeA size is : 4
class c. before redef. typeA size is : 1
class c. after redef. typeA size is : 1
c::typeA size = 1, b::typeA size = 4, a::typeA size = 4
请按任意键继续. . .
相关文章推荐
- 关于第三方cookie的作用域以及针对用户行为的使用
- 关于第三方cookie的作用域以及针对用户行为的使用
- 关于js中单引号(')和双引号(")的使用以及转义的理解
- objective-c中对象所有权的内存管理(关于set,get方法),以及如何使用@property来进行简易操作(九)
- php开发过程中关于继承的使用方法
- 关于继承的使用
- 三层交换机关于VLAN的划分以及ACL的使用
- 关于CS plus for CC 的使用以及注意事项
- 关于char以及varchar2在实际使用时的一点经验
- 关于动态获取设置像素大小以及TypedValue.applyDimension()的使用
- C语言中的复杂声明和定义,以及如何使用typedef进行简化
- 关于miniconda的安装,配置以及包批量安装和使用
- Spring学习笔记 关于Bean定义的重用-使用abstract以及parent标签属性
- 安卓开发之商品展示--关于Listview的使用以及数据库的增删改查
- [cocos2d-x]关于update回调,以及继承后子类调用父类的Update处理的问题
- 关于C++中函数指针的使用(包含对typedef用法的讨论)
- 关于js中获得某一天的昨天以及上周一天的日期使用问题
- 关于CocoaPod无法升级以及无法下载第三方库和Cocoapods在OS X 10.11系统上的无法正常使用的问题
- SurfaceView(包含对消息传递的回顾以及对继承View类使用的回顾)制作指南针
- 关于Java Web使用的DataTables插件获取所有数据以及表格刷新