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. Invalid prop: type check failed for prop "xxx". Expected Number, got String.

    在子组件progress-circle.vue的template中的定义如下: <svg :width="radius" :height="radius" ...

  2. OpenXml SDK学习笔记(4):设置文件级别的样式

    观察上一段日记最后的代码: 这里的样式基本可以理解为行内CSS.那么既然有行内的样式,就肯定有外部的样式.那这部分就对应笔记1里说的style.xml文件.这个文件对应的是Document.MainD ...

  3. JAVA POI导出EXCEL 动态表头、多级表头、动态数据

    导出Excel文件是业务中经常遇到的需求,以下是经常遇到的一些问题: 1,导出中文文件名乱码 String filename = "sheet1";response.setChar ...

  4. OpenShift S2I 概念及流程

    S2I 概念 S2I(Source To Image)即从源码到镜像的一个过程,OpenShift 将它作为基础功能提供给用户,包含 S2I CLI 工具 与 S2I 流程.通过这些工具和既定流程,能 ...

  5. soft and hard limit

    soft限制了资源使用上限; soft可调整; hard限制了soft上限; 普通用户可使用ulimit -H调低hard limit. 限制的是一个进程可用资源, 而不是某个用户总和. man se ...

  6. [cf1349D]Slime and Biscuits

    枚举最终的获得所有饼干的人$i$(以下简称"获胜者"),对于$i$获胜的情况,令其贡献为游戏轮数,否则其贡献为0,记$F_{i}$为期望贡献(即所有情况概率*贡献之和),答案即为$ ...

  7. [loj2304]泳池

    将等于$k$差分,即小于等于$k$减去小于等于$k-1$,由于两者类似,不妨仅考虑前者 令$f_{i,j}$表示仅考虑$i$列(即$n=i$时),若前$j$行都没有障碍,此时最大面积小于等于$k$的概 ...

  8. [loj3366]嘉年华奖券

    联系绝对值的几何意义/分类讨论,不难发现若$n$张奖券上的数从小到大依次为$a_{i}$,则收益为$\sum_{i=1}^{\frac{n}{2}}a_{i+\frac{n}{2}}-a_{i}$ 假 ...

  9. [bzoj1391]order

    考虑最小割,即最少要去掉多少收益先S向所有机器连边,流量为购买费用:所有机器向工作连边,流量为租借费用:工作向T连边,流量为收益那么对于每一个工作,要么割掉连向T的边,要么购买/租借所有机器,同时由于 ...

  10. vue实现聊天+图片表情功能

    项目需求是这样的:要求实现类似于微信聊天一样,表情+文字效果 "文字效果" 表情包三种方案 表情包的实现其实可以分为以下三种情况: 表情包:点击表情--直接发送大表情(这种方案其实 ...