C++ class与内存

//类名 变量名 栈区

//类名 *指针名 = 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值