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?的更多相关文章

  1. 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 ...

  2. Oracle Schema Objects(Schema Object Storage And Type)

    One characteristic of an RDBMS is the independence of physical data storage from logical data struct ...

  3. C# typeof() 和object.GetType() 、Type..GetType()使用和区别

    进行学习到表达树了,用动Tpye了.所以整理了以下他们区别和用法 总得来说他们都是为了获取某个实例具体引用的数据类型System.Type.1.GetType()方法继承自Object,所以C#中任何 ...

  4. MVC5添加控制器总是报“Multiple object sets per type are not supported”

    http://www.asp.net/mvc/tutorials/mvc-5/introduction/creating-a-connection-string 按照上面的指导做练习,  总报小面的错 ...

  5. 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.

  6. isinstance(object, classinfo) class type(name, bases, dict)

    w https://docs.python.org/3/library/functions.html#isinstance

  7. 理解C# 4 dynamic(1) - var, object, dynamic的区别以及dynamic的使用

    阅读目录: 一. 为什么是它们三个 二. 能够任意赋值的原因 三. dynamic的用法 四. 使用dynamic的注意事项 一. 为什么是它们三个? 拿这三者比较的原因是它们在使用的时候非常相似.你 ...

  8. 利用Object.prototype.toString方法,实现比typeof更准确的type校验

    Object.prototype.toString方法返回对象的类型字符串,因此可以用来判断一个值的类型. 调用方法: Object.prototype.toString.call(value) 不同 ...

  9. Object.notifyAll()

    void java.lang.Object.notifyAll() Causes all threads which are waiting on this object's monitor (by ...

随机推荐

  1. JMeter学习笔记--录制脚本(一)

    ---------------------------------------------------------------------------------------------------- ...

  2. 关于JDBC中查询方法的抽取

    萌新的JAVA学习笔记[1] 先来张伊蕾娜镇场~~ 简单描述 起初我们的查询方法时分为单个查询和全部查询,过于局限与繁琐,如此一来我们能不能想一个办法将所有类型的查询抽取出来并整合成为一个单独的工具方 ...

  3. F5 BIG-IP 远程代码执行RCE(CVE-2020-5902)复现

    漏洞简介 F5 BIG-IP 是美国``F5公司一款集成流量管理.DNS.出入站规则.web应用防火墙.web网关.负载均衡等功能的应用交付平台. 在F5 BIG-IP产品的流量管理用户页面 (TMU ...

  4. Python知识整理(二)

    6.高级特性--简化代码量 1.切片 L[0:3]表示,从索引0开始取,直到索引3为止,但不包括索引3.即索引0,1,2,正好是3个元素. 如果第一个索引是0,还可以省略:L[:3] Python支持 ...

  5. SpringCloud微服务实战——搭建企业级开发框架(十七):Sentinel+Nacos配置持久化

      Sentinel Dashboard中添加的规则是存储在内存中的,我们的微服务或者Sentinel一重启规则就丢失了,现在我们将Sentinel规则持久化配置到Nacos中,在Nacos中添加规则 ...

  6. Chrome handless无界面浏览器的脚本操作

    1.什么是Phantomjs (已经停止更新) 是一个无界面的浏览器 支持页面元素查找,js的执行等 由于不进行css和gui渲染,运行效率要比真实的浏览器要快很多 2.如何使用Phantomjs? ...

  7. 如何用webgl(three.js)搭建一个3D库房,3D密集架,3D档案室(升级版)

    很长一段时间没有写3D库房,3D密集架相关的效果文章了,刚好最近有相关项目落地,索性总结一下 与之前我写的3D库房密集架文章<如何用webgl(three.js)搭建一个3D库房,3D密集架,3 ...

  8. [Aizu2993]Invariant Tree

    若$(i,j)\in E$,实际上会不断推出$(p_{i},p_{j})\in E,(p_{p_{i}},p_{p_{j}})\in E,...$ 考虑将$i$向$p_{i}$连边得到了一张(由若干个 ...

  9. [luogu7736]路径交点

    对于两条路径,注意到每一个交点都会改变两者的上下关系,因此两条路径交点的奇偶性,仅取决于两者的起点和终点是否改变了上下关系(改变即为奇数) 类似地,对于整个路径方案,令$p_{i}$为以第一层的$i$ ...

  10. [loj3524]钥匙

    由于到达关系具有传递性,可以考虑不断将若干个可以相互到达的点缩点,并且当两个点只能单向到达时,能到达另一个点的点一定不是最小值 由此,我们来考虑dfs,即不断从一个节点开始,遍历其可以到达的点,当发现 ...