#include<iostream>
using namespace std;
//成員變量 和 成員函數(shù) 分開(kāi)儲(chǔ)存的
class Person {
public:
?? ?Person() {
?? ??? ?mA = 0;
?? ?}
?? ?//非靜態(tài)成員變量占對(duì)象空間
?? ?int mA;
?? ?//靜態(tài)成員變量不占對(duì)象空間
?? ?static int mB;
?? ?//函數(shù)也不占對(duì)象空間,所有函數(shù)共享一個(gè)函數(shù)實(shí)例
?? ?void func() {
?? ??? ?cout << "mA:" << this->mA << endl;
?? ?}
?? ?//靜態(tài)成員函數(shù)也不占對(duì)象空間
?? ?static void sfunc() {
?? ?}
};
int Person::mB = 0;
void test01()
{
?? ?Person p;
?? ?//空對(duì)象占用內(nèi)存空間:1
?? ?//C++編譯器會(huì)給每個(gè)空對(duì)象也分配一個(gè)字節(jié)空間,是為了區(qū)分空對(duì)象占內(nèi)存的位置
?? ?//每個(gè)空對(duì)象也應(yīng)該有一個(gè)獨(dú)一無(wú)二的內(nèi)存地址
?? ?cout <<"size of p =" <<sizeof(p) << endl;
}
void test02()
{
?? ?Person p;
?? ?cout <<"size of p =" <<sizeof(p) << endl;
}
int main() {
?? ?//test01();
?? ?test02();
?? ?system("pause");文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-828521.html
?? ?return 0;
}文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-828521.html
#include<iostream>
using namespace std;
class Person
{
public:
?? ?Person(int age)
?? ?{
?? ??? ?//1、當(dāng)形參和成員變量同名時(shí),可用this指針來(lái)區(qū)分
?? ??? ?//this指針指向 被調(diào)用的成員函數(shù) 所屬的對(duì)象
?? ??? ?this->age = age;
?? ?}
?? ?Person& PersonAddPerson(Person p)
?? ?{
?? ??? ?this->age += p.age;
?? ??? ?//返回對(duì)象本身
?? ??? ?//this指向p2的指針,而*this指向的就是p2這個(gè)對(duì)象的本體
?? ??? ?return *this;
?? ?}
?? ?int age;
};
void test01()
{
?? ?//1.解決名稱(chēng)沖突
?? ?Person p1(10);
?? ?cout << "p1.age = " << p1.age << endl;
?? ?//2.返回對(duì)象本身用*this
?? ?Person p2(10);
?? ?//鏈?zhǔn)骄幊趟枷?br> ?? ?p2.PersonAddPerson(p1).PersonAddPerson(p1).PersonAddPerson(p1);
?? ?cout << "p2.age = " << p2.age << endl;
}
int main() {
?? ?test01();
?? ?system("pause");
?? ?return 0;
}
#include<iostream>
using namespace std;
//空指針調(diào)用成員函數(shù)
class Person
{
public:
?? ?void ShowClassName()
?? ?{
?? ??? ?cout << "我是Person類(lèi)!" << endl;
?? ?}
?? ?void ShowPersonAge()
?? ?{
?? ??? ?if (this == NULL)
?? ??? ?{
?? ??? ??? ?return;
?? ??? ?}
?? ??? ?//報(bào)錯(cuò)原因是傳入的指針為NULL
?? ??? ?cout << this->mAge << endl;
?? ?}
public:
?? ?int mAge;
};
void test01()
{
?? ?Person * p = NULL;
?? ?p->ShowClassName(); //空指針,可以調(diào)用成員函數(shù)
?? ?p->ShowPersonAge();? //但是如果成員函數(shù)中用到了this指針,就不可以了
}
int main() {
?? ?test01();
?? ?system("pause");
?? ?return 0;
}
#include<iostream>
using namespace std;
//常函數(shù)
class Person
{
public:
?? ?Person()
?? ?{
?? ??? ?m_A = 0;
?? ??? ?m_B = 0;
?? ?}
?? ?//this指針的本質(zhì)是一個(gè)指針常量,指針的指向不可修改
?? ?//const Person * const this
?? ?//在成員函數(shù)后面加const,修飾的是this指向,讓指針指向的值也不可以修改
?? ?void ShowPerson() const
?? ?{
?? ??? ?//this = NULL;??? //this指針不可以修改指針的指向?? Person* const this;
?? ??? ?//this->mA = 100; //但是this指針指向的對(duì)象的數(shù)據(jù)是可以修改的
?? ??? ?//const修飾成員函數(shù),表示指針指向的內(nèi)存空間的數(shù)據(jù)不能修改,除了mutable修飾的變量
?? ??? ?this->m_B = 100;
?? ?}
?? ?void MyFunc()
?? ?{
?? ??? ?mA = 10000;
?? ?}
public:
?? ?int m_A;
?? ?mutable int m_B; //特殊變量,即使在常函數(shù)中,也可以修改這個(gè)值
};
//const修飾對(duì)象? 常對(duì)象
void test01() {
?? ?const Person person; //在對(duì)象前加const,變?yōu)槌?duì)象 ?
?? ?cout << person.m_A << endl;
?? ?//person.mA = 100; //常對(duì)象不能修改成員變量的值,但是可以訪(fǎng)問(wèn)
?? ?person.m_B = 100;? //但是常對(duì)象可以修改mutable修飾成員變量
?? ?//常對(duì)象只能調(diào)用常函數(shù)
?? ?person.ShowPerson();
?? ?//person.MyFunc(); //常對(duì)象 不能調(diào)用普通成員函數(shù),因?yàn)槠胀ǔ蓡T函數(shù)可以修改屬性
}
int main() {
?? ?test01();
?? ?system("pause");
?? ?return 0;
}
到了這里,關(guān)于C++類(lèi)和對(duì)象-C++對(duì)象模型和this指針->成員變量和成員函數(shù)分開(kāi)存儲(chǔ)、this指針概念、空指針訪(fǎng)問(wèn)成員函數(shù)、const修飾成員函數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!