Can a C++ class have an object of self type?
A class declaration can contain static object of self type,it can also have pointer to self type,but it can not have a non-static object of self type。
例如,下面的程序可运行。
1 // A class can have a static member of self type
2 #include<iostream>
3
4 using namespace std;
5
6 class Test
7 {
8 static Test self; // works fine
9
10 /* other stuff in class*/
11
12 };
13
14 int main()
15 {
16 Test t;
17 getchar();
18 return 0;
19 }
下面的程序也可运行。
1 // A class can have a pointer to self type
2 #include<iostream>
3
4 using namespace std;
5
6 class Test
7 {
8 Test *self; // works fine
9
10 /* other stuff in class*/
11
12 };
13
14 int main()
15 {
16 Test t;
17 getchar();
18 return 0;
19 }
但是,下面的程序会产生编译错误"field `self’ has incomplete type“。
在VC 6.0下会产生编译错误"error C2460: 'self' : uses 'Test', which is being defined"。
1 // A class can not have a non-static member of self type
2 #include<iostream>
3
4 using namespace std;
5
6 class Test
7 {
8 Test self; // works fine
9
10 /* other stuff in class*/
11
12 };
13
14 int main()
15 {
16 Test t;
17 getchar();
18 return 0;
19 }
If a non-static object is member then declaration of class is incomplete and compiler has no way to find out size of the objects of the class.
如果在类的声明中,non-static对象是该类的成员,若该non-static对象是incomplete,那么,编译器没有办法获取该类对象的大小。
static variables do not contribute to the size of objects. So no problem in calculating size with static variables of self type.
由于类的对象的大小是由non-static数据成员构成的,当然, 这里仅仅是无继承的、无虚函数的类。所以static数据成员不构成类的对象的大小,因此,当类的声明中包含有该类的static objects时是没有问题的。
For a compiler, all pointers have a fixed size irrespective of the data type they are pointing to, so no problem with this also.
对于编译器而言,所有类型的指针有固定的大小(32位机器下4字节),与指针所指对象的数据类型没有关系,所以类声明中包含自身类型的指针是没有问题的。
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
转载请注明:http://www.cnblogs.com/iloveyouforever/
2013-11-25 20:14:30
Can a C++ class have an object of self type?的更多相关文章
- The method format(String, Object[]) in the type String is not applicable for the arguments
今天,我弟遇到一个有意思的错误~ 程序: package com.mq.ceshi1; public class StringFormat { public static void main(Stri ...
- Oracle Schema Objects(Schema Object Storage And Type)
One characteristic of an RDBMS is the independence of physical data storage from logical data struct ...
- C# typeof() 和object.GetType() 、Type..GetType()使用和区别
进行学习到表达树了,用动Tpye了.所以整理了以下他们区别和用法 总得来说他们都是为了获取某个实例具体引用的数据类型System.Type.1.GetType()方法继承自Object,所以C#中任何 ...
- MVC5添加控制器总是报“Multiple object sets per type are not supported”
http://www.asp.net/mvc/tutorials/mvc-5/introduction/creating-a-connection-string 按照上面的指导做练习, 总报小面的错 ...
- The method queryForMap(String, Object...) from the type JdbcTemplate refers to the missing type DataAccessException
Add spring-tx jar of your spring version to your classpath.
- isinstance(object, classinfo) class type(name, bases, dict)
w https://docs.python.org/3/library/functions.html#isinstance
- 理解C# 4 dynamic(1) - var, object, dynamic的区别以及dynamic的使用
阅读目录: 一. 为什么是它们三个 二. 能够任意赋值的原因 三. dynamic的用法 四. 使用dynamic的注意事项 一. 为什么是它们三个? 拿这三者比较的原因是它们在使用的时候非常相似.你 ...
- 利用Object.prototype.toString方法,实现比typeof更准确的type校验
Object.prototype.toString方法返回对象的类型字符串,因此可以用来判断一个值的类型. 调用方法: Object.prototype.toString.call(value) 不同 ...
- Object.notifyAll()
void java.lang.Object.notifyAll() Causes all threads which are waiting on this object's monitor (by ...
随机推荐
- JMeter学习笔记--性能测试理论
一.性能测试技能树 二.性能测试流程 三.性能测试相关术语 性能测试指标就是: 多(并发量)快(响应时间)好(稳定性[长时间运行])省(资源使用率).思考时间 1.负载 模拟业务操作对服务器造成压力的 ...
- fabric运行记录
创建第一个fabric网络 Generate Network Artifacts cd first-network 先关闭服务 ./byfn.sh -m down 然后创建 ./byfn.sh -m ...
- 问题 K: A/B Problem
题目描述 做了A+B Problem,A/B Problem不是什么问题了吧! 输入 每组测试样例一行,首先一个号码A,中间一个或多个空格,然后一个符号( / 或者 % ),然后又是空格,后面又是一个 ...
- 『学了就忘』Linux软件包管理 — 44、在RPM包中提取文件
目录 1.RPM包中文件的提取 2.在RPM包中提取文件的操作 (1)cpio命令介绍 (2)提取RPM包中文件 1.RPM包中文件的提取 为什么要做这个事呢? 在操作Linux系统的时候误删除一个文 ...
- [bzoj1863]皇帝的烦恼
二分枚举答案,假设是ans,考虑判定答案从前往后计算,算出每一个将军与第一个将军最少和最多有多少个相同的奖牌,贪心转移即可 1 #include<bits/stdc++.h> 2 usin ...
- while,do...while及for三种循环结构
循环结构 while循环 while (布尔表达式) { //循环内容 } 只要布尔表达式为true循环就会一直执行 我们大多数情况会让循环停止下来,需要一个让表达式失效的方式来停止循环 while循 ...
- 使用微软RPA工具 Power Automate自动完成重复性工作
介绍 最近发现了win11自带了一个有趣的功能,可以自动去执行一些流程的工作.恰好目前每天早上都需要去提醒同事填写日计划,刚好可以试用下. 这是官网上对此功能的介绍 可以看到,对于win11我们是可以 ...
- C++ 编译错误记录
C++ _ZSt28__throw_bad_array_new_lengthv1 编译错误 出现场景:类似代码 vector<vector<int>> grid = {{1, ...
- CF45G
考虑哥德巴赫猜想:一个偶数可以被拆分两个质数. 所以我们考虑如果不是偶数的话,我们拆分成\((2,m-2)\)或者\((3,del(m - 3))\) 如果是偶数的话\(del(m)\),我们直接枚举 ...
- CF1539F Strange Array
这玩意为啥是紫. 考虑对每个小于\(x\)的数值设为1,大于\(x\)的数值设为-1. 那么对于答案要求的就是绝对值最大的连续段. 线段树是很显然的. 考虑我们不能对每个数都进行一遍重构,这样就退化到 ...