C++之const修饰得到是谁
来源:互联网 发布:php在线代码编辑器 编辑:程序博客网 时间:2024/04/30 18:04
在类中的函数中定义一个const类的函数 那么const到底修饰的是谁
#include <iostream>using namespace std;//C++是如何管理普通成员函数还有静态成员函数以及静态成员数据的//C++类对象中的成员变量和成员函数是分开存储的//普通成员变量存储在对象中 于 struct具有相同的内存布局和字节对齐方式 //静态成员变量存储于全局数据区中//成员函数:存储于代码中//C++中类的普通成员函数都隐士的包含了一个指向当前对象的this指针//静态成员函数、成员变量属于类//静态成员函数与普通成员函数的成员函数的区别//静态成员函数不包含指向具体对象的指针//普通成员函数包含指向具体成员函数的指针 class Test { public: Test(int a,int b) //Test(Test * this,int a,intb) { this->a = a; //当参数的名称和类变量名称相同的时候 可以使用this指针 this->b =b; }; void printT() { //类中可以使用this指针 cout << this->a << endl; cout << this->b <<endl; } protected: private: int a; int b; };int main(){ cout << "hello world!" << endl; system("pause"); return 0;}
1>------ 已启动生成: 项目: demotest, 配置: Debug Win32 ------1>正在编译...1>demo_16.cpp1>正在链接...1>正在嵌入清单...1>生成日志保存在“file://e:\VS2008\demotest\demotest\Debug\BuildLog.htm”1>demotest - 0 个错误,0 个警告========== 生成: 成功 1 个,失败 0 个,最新 0 个,跳过 0 个 ==========
#include <iostream>using namespace std;//C++是如何管理普通成员函数还有静态成员函数以及静态成员数据的//C++类对象中的成员变量和成员函数是分开存储的//普通成员变量存储在对象中 于 struct具有相同的内存布局和字节对齐方式 //静态成员变量存储于全局数据区中//成员函数:存储于代码中//C++中类的普通成员函数都隐士的包含了一个指向当前对象的this指针//静态成员函数、成员变量属于类//静态成员函数与普通成员函数的成员函数的区别//静态成员函数不包含指向具体对象的指针//普通成员函数包含指向具体成员函数的指针 class Test { public: Test(int a,int b) //Test(Test * this,int a,intb) { this->a = a; //当参数的名称和类变量名称相同的时候 可以使用this指针 this->b =b; }; void printT() const { //类中可以使用this指针 this->a = 200; //demo_16.cpp(27) : error C2166: 左值指定 const 对象 cout << this->a << endl; cout << this->b <<endl; } protected: private: int a; int b; };int main(){ cout << "hello world!" << endl; system("pause"); return 0;}
在进入深入的解析发现:;
#include <iostream>using namespace std;//C++是如何管理普通成员函数还有静态成员函数以及静态成员数据的//C++类对象中的成员变量和成员函数是分开存储的//普通成员变量存储在对象中 于 struct具有相同的内存布局和字节对齐方式 //静态成员变量存储于全局数据区中//成员函数:存储于代码中//C++中类的普通成员函数都隐士的包含了一个指向当前对象的this指针//静态成员函数、成员变量属于类//静态成员函数与普通成员函数的成员函数的区别//静态成员函数不包含指向具体对象的指针//普通成员函数包含指向具体成员函数的指针 class Test { public: Test(int a,int b) //Test(Test * this,int a,intb) { this->a = a; //当参数的名称和类变量名称相同的时候 可以使用this指针 this->b =b; }; void const printT() const { //类中可以使用this指针 this->a = 200; //demo_16.cpp(27) : error C2166: 左值指定 const 对象 cout << this->a << endl; cout << this->b <<endl; } protected: private: int a; int b; };int main(){ cout << "hello world!" << endl; system("pause"); return 0;}
其实C++编译器实现的过程是这样的,在函数后面写上 const的时候C++编译器会将其按照下面的方式进行处理
void printT() const //<==> void printT(const Test *const this) { //类中可以使用this指针 this->a = 200; //demo_16.cpp(27) : error C2166: 左值指定 const 对象 cout << this->a << endl; cout << this->b <<endl;}
在实际的情况下多一个const因为在设C++编译器的时候C++编译器的设计者已经将this指针设计成了const类型的指针了 认为的加上的const是修饰this指针指向变量是一个const类型的变量
void printT() const //<==> void printT(const Test *const this)
阅读全文
0 0
- C++之const修饰得到是谁
- const 修饰的是谁
- const修饰的是谁
- C语言之const修饰符
- C语言之const关键字修饰
- C语言之const修饰符
- C修饰符const
- 成员函数的const到底修饰的是谁
- 修饰符之const
- [c/c++]const修饰指针
- C语言的那些小秘密之const修饰符
- C语言的那些小秘密之const修饰符
- C语言的那些小秘密之const修饰符
- C语言的那些小秘密之【const修饰符】
- C语言的那些小秘密之const修饰符
- c/c++ const修饰符
- Objective-C修饰符const
- C语言const的修饰
- 桶排序
- 基本MarkDown语法 引言
- EOJ Monthly 2017.12 (暨 ECNU 12 月内部选拔) B题
- rails中的request
- [Leetcode] 523. Continuous Subarray Sum 解题报告
- C++之const修饰得到是谁
- 网易游戏笔试题(3) 20171209
- 链接文件
- 单源最短路径的迪克斯特拉(Dijkstra)算法
- PHP中被忽略的性能优化利器:生成器
- u-boot串口和stdio、console初始化及相关操作详解<三>
- 667. Beautiful Arrangement II
- python秒搭本地服务器
- 5、权限管理