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 ...
随机推荐
- vue禁用浏览器回退
解决方案 mounted() { history.pushState(null, null, document.URL) window.addEventListener('popstate', () ...
- Windows下git多用户配置
refer from :https://blog.csdn.net/qq_39892503/article/details/109374201 windows git多用户配置 在安装git结束,我们 ...
- NOIP模拟92&93(多校26&27)
前言 由于太菜了,多校26 只改出来了 T1 ,于是直接并在一起写啦~~~. T0 NOIP 2018 解题思路 第一次考场上面写三分,然而我并不知道三分无法处理不是严格单峰的情况,但凡有一个平台都不 ...
- AppGallery Connect场景化开发实战—注册订阅通知
借助AppGallery Connect(以下简称AGC)的认证服务,云函数,短信服务等服务,当用户注册成功后,便可以在注册的手机号或者邮箱地址中收到一条应用的欢迎短信或者欢迎邮件.以便让开发者更快地 ...
- 【JAVA】笔记(7)--- 数组精讲
数组的静态初始化: 1.一维数组: int [ ] arr = { 1,2,3,4 } ; Object [ ] arr = { new Object ( ) , new Object ( ) , ...
- Python基础(序列化)
#pickling import pickle,json # d = dict(name='傻狗1',age=300,score=100) # d1 = pickle.dumps(d)#pickle. ...
- 大爽Python入门教程 0-3 安装Atom与配置Python环境
大爽Python入门公开课教案 点击查看教程总目录 安装轻量级IDE--Atom 一 下载 下面步骤1,2中网络卡顿的朋友, 请直接查看步骤3来操作 1. 搜索官网 使用搜索引擎搜索Atom, 打开搜 ...
- GO语言数据结构之链表
链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成.每个结点包括两个部分: ...
- 重写(Override)与重载(Overload)区别
重写是子类对父类的允许访问的方法的实现过程进行重新编写. 方法重写三要素: (1)方法名形参列表相同: (2)返回值类型和声明异常类型子类小于父类: (3)访问权限,子类大于等于父类. 重写的好处在于 ...
- 析构函数与this指针
二.析构函数 1.知识点介绍 析构函数和构造函数一样,也是一种特殊的函数,主要的作用是在对象结束生命周期时,系统自动调用析构函数,来做一些清理工作,比如在对象中有申请内存,那么是需要自己去释放内存的, ...