3.22 c++提高 4day

1.对象模型
成员函数不占用对象的内存 -
那么他们如何找到对象的成员呢 因为虽然不是默认传参 但是编译器会传入 this指针 来指向成员
21: a.test();
007E40B7 lea ecx,[a]
007E40BA call to::test (07E13BBh)

2.this -》this指针
1.使得对应的成员函数找到成员
2.变量形参同名
this.a=a;

2.返回对象本身

include

using namespace std;
cclass to
{
public:
int a = 10;
void test(int a)
{
this->a = a;
}
to retun()
{
return *this;
}
};
int main()
{
to a;
a.retun();

}

问题
1.静态成员变量(类)
printf("%d", to::a);
002A1921 mov eax,dword ptr [to::a (02AA014h)]
002A1926 push eax
不占用成员函数的大小 由一个固定地址保存
2.this指向
//this==calssname * const this 指向无法改变
3.常函数
1.无法修改成员变量
2.可以修改静态成员变量--不在其成员中是可以改的
3.不能修改this指向的内容
const 类 const this;
class to
{
public:
static int a;
int b;
int c;
void test() const
{

	a = 100;

}

};
int to::a = 0x122;
int main()
{
to pp;
pp.test();
printf("%d", to::a);

}

1.常对象
1.不能改变常成员对象的值
class to
{
public:
const int a = 100;
void test()
{
printf("%d", a);
}
};

int main()
{
to pp;

}

友元 关系
友元函数用途
1.运算符重载的某些场合
2.外部函数需要访问私有成元 或者类与类交换
3.传参访问私有 类内指针访问
class to
{
friend void test();
public:
string rty = "卧室";

private:
string book = "卫生间";

};

void test()
{
to zz;
printf("%s", zz.book);

}
int main()
{
test();

}
友元类

include

using namespace std;

class to
{
friend class to2 ;
friend void test();
public:
string rty = "卧室";

private:
int book = 12;

};

class to2
{
public:
void test(to* mu)
{
printf("%d", mu->book);
}

};

int main()
{
to qq;
to2 zz;
zz.test(&qq);

}

类的友元函数

友元注意
1.关系不能继承 2.单向关系 3.无传递性

单例模式
1.一个类只能实例化一个对象

include

using namespace std;

class to
{
public:
static to* zza()
{
return zz;
}
private:
to()
{
printf("我是0");
}
~to()
{
printf("我是1");
}
to(const to& m)
{
printf("我是2");
}
static to* zz;
};

to* to::zz = new to;
int main()
{
/// 获取到开辟的对象
to* qut = to::zza();
}

posted @ 2023-03-22 22:02  逆向狗  阅读(16)  评论(0)    收藏  举报