C++ class与内存
来源:互联网 发布:网页页面设计软件 编辑:程序博客网 时间:2024/04/25 21:05
//类名 变量名 栈区
//类名 *指针名 = new 类名 堆区
//类的静态成员 静态区
//类的成员函数,静态函数都在代码区,类的函数都是共享的
//代码共享,所有的类对象共享代码
//const变量在类的外部,一开始必须初始化,用常量强行替换,不读内存
//const变量在类的内部,必须构建一个类才能初始化,const与C语言里的const是一样的
//常规创建类对象的时候在栈上,new创建对象的时候,在堆上
//引用本质上就是变量的别名,4个字节,本质上是一个指针
//引用变量跟其他变量一样,常规创建类对象的时候在栈上,new创建对象的时候,在堆上
//static const int dashu;//静态常量区
//静态常量区可以访问,但是不能修改,可以用注射的方式修改
#include <iostream>#include <functional>using namespace std;using namespace placeholders;class myclass{public: int num; int data; int *p; const int coint; int &myint; static int shu; static const int dashu;//静态常量区public: static void go() { cout << "hehehe" << endl; } void run() { cout << "runrunrun" << endl; } myclass(int a, int b) :coint(a), myint(b) { cout << &a << " " << &b << endl; cout << &coint << " " << &myint << endl; const int *p = &coint; cout << *p << " " << coint << endl; int *px = const_cast<int*>(p); *px = 12; cout << coint << " " << *px << endl; } ~myclass() { }};int myclass::shu = 0;//初始化const int myclass::dashu = 20;void main(){ myclass myclass1(10,9); void(myclass::*p)() = &myclass::run;//传统函数指针调用,代码共享,所有的类对象共享代码 void(*p1)() = &myclass::go;//函数指针调用静态函数,go auto func1 = bind(&myclass::run, &myclass1);//仿函数调用run func1(); const int *px = &myclass::dashu; int *ppx = const_cast<int*>(px); *ppx = 12;//静态常量区可以访问,但是不能修改,可以用注射的方式修改 cout << *ppx << " " << *px << " " << myclass::dashu << endl; cin.get();}
0 0
- C++ class与内存
- C的存储类,链接与内存管理(Storage Class, Linkage, Memory Management)
- C/C++ struct/class/union内存对齐
- C/C++ struct/class/union内存对齐
- C/C++ struct/class/union内存对齐
- TypeName 与 class (C++)
- C++继承与Class 内存模型
- C与内存
- c与内存
- C指针与内存
- (基础) class 与 c 语言对比学习-OC+class
- Class.isAssignableFrom(Class c)与instanceof关键字有何区别
- [C#]class 与 struct 的区别
- 有关abstract class 与 interface 论 (c#)
- 有关abstract class 与 interface 再论 (c#)
- C#/WPF学习笔记:class 与 struct
- JNI Java class与C struct对应
- C++:struct与class的区别
- POJ 3641 Pseudoprime numbers
- LeetCode(20)Valid Parentheses
- GC垃圾收集算法
- Grep用法
- hdu 3593
- C++ class与内存
- LeetCode(141)(142) Linked List Cycle I II
- Linux - 有效群组(effective group)与初始群组(initial group),groups,newgrp
- 高仿SinaWeibo新浪微博发布页面话题效果
- 分享在MVC3.0中使用jQuery DataTable 插件
- FileOutputStream/FileInputStream--FileWriter/FileReader
- 相似图片搜索
- 提示磁盘被写保护怎么办?
- Python中re(正则表达式)模块详解