C++复习 -- 常用关键字


this 关键字:

概念


在 C++ 中, this 关键字是一个指向调用对象的指针。它在成员函数内部使用,用于引用调用该函数的对象。使用 this 可以明确指出成员函数正在操作的是哪个对象的数据成员。

=========


case :证明他就是一个指向被调用对象的指针:

#include <iostream>
#include <string>
using namespace std;

class Car {
private:
string brand;
int year;
public:
Car(string bb,int yy){
cout<<"构造函数里面:"<<endl;
cout<<this<<endl;
}

};

int main()
{
Car c1("mmx",1999);
cout<<&c1<<endl;
    return 0;
}


------------------------------------

case2 -- 体现this的应用


应用1: 进行构造初始化的时候 ,传入参数和实际变量重名 --   加上this 关键字修饰,避免歧义
应用2 -- 返回当前对象的引用:

// 返回引用 -- 方便 进行链式调用:

#include <iostream>
#include <string>
using namespace std;

class Car {
private:
string brand;
int year;
public:
Car(string brand,int year){
//cout<<"构造函数里面:"<<endl;
//cout<<this<<endl;
this->brand = brand;
this->year = year;
}
void display() const {
cout << "Brand: " << this->brand << ", Year: " << this->year << endl;
// 也可以不使用 this->,直接写 brand 和 year
}
Car& setYear(int year){
    this ->year =year; //修改年份
    return *this; // 返回被调用对象的引用
}

};

int main()
{
Car c1("mmx",1999);
c1.display();
//链式调用:
c1.setYear(2003).display();

    return 0;
}

在这个例子中, Car 类的构造函数使用 this 指针来区分成员变量和构造函数参数。同样, setYear
成员函数使用 this 指针来返回调用该函数的对象的引用,这允许链式调用,如
myCar.setYear(2021).display(); 。在 main 函数中创建了 Car 类型的对象,并展示了如何使用这
些成员函数。

======================================================

new 关键字:

概念:

new 关键字用于动态分配内存。它是C++中处理动态内存分配的主要工具之一,允许在程序运
行时根据需要分配内存。

基本用法


分配单个对象:使用 new 可以在动态分配一个对象。例如, new int 会分配一个 int 类型的空
间,并返回一个指向该空间的指针。
分配对象数组: new 也可以用来分配一个对象数组。例如, new int[10] 会分配一个包含10个整数的数组。
初始化:可以在 new 表达式中使用初始化。对于单个对象,可以使用构造函数的参数:

与 delete 配对使用
使用 new 分配的内存必须显式地通过 delete (对于单个对象)或 delete[] (对于数组)来释放,以
避免内存泄露:
释放单个对象:
释放数组:


注意事项


异常安全:如果 new 分配内存失败,它会抛出 std::bad_alloc 异常(除非使用了 nothrow 版
本)。
内存泄露:忘记释放使用 new 分配的内存会导致内存泄露。
匹配使用 delete 和 delete[] :为避免未定义行为,使用 new 分配的单个对象应该使用
delete 释放,使用 new[] 分配的数组应该使用 delete[] 释放。


示例代码


int* ptr = new int; //C语言中,int *p = (int *)malloc(sizeof(int));
int* arr = new int[10]; //C语言中,int *arr = (int *)malloc(sizeof(int)*10);
MyClass* obj = new MyClass(arg1, arg2);
delete ptr; // 释放 ptr 指向的对象
delete[] arr; // 释放 arr 指向的数组

case

class MyClass {
public:
MyClass() {
std::cout << "Object created" << std::endl;
}
};
int main() {
// 分配单个对象
MyClass* myObject = new MyClass();
// 分配对象数组
int* myArray = new int[5]{1, 2, 3, 4, 5};
// 使用对象和数组...
// 释放内存
delete myObject;
delete[] myArray;
return 0;
}

在这个例子中, new 被用来分配一个 MyClass 类型的对象和一个整数数组,然后使用 delete 和
delete[] 来释放内存。每个 new 都对应一个 delete ,保证了动态分配的内存被适当管理。


=========================================================


静态成员  -- static 关键字:

概念

静态成员在C++类中是一个重要的概念,它包括静态成员变量和静态成员函数。

静态成员的特点和存在的意义:


静态成员变量


1. 定义:静态成员变量是类的所有对象共享的变量。与普通成员变量相比,无论创建了多少个类的实例,静态成员变量只有一份拷贝
2. 初始化:静态成员变量需要在类外进行初始化,通常在类的实现文件中。
3. 访问:静态成员变量可以通过类名直接访问,不需要创建类的对象。也可以通过类的对象访问。
4. 用途:常用于存储类级别的信息(例如,计数类的实例数量)或全局数据需要被类的所有实例共
享。


静态成员函数


1. 定义:静态成员函数是可以不依赖于类的实例而被调用的函数。它不能访问类非静态成员变量和非静态成员函数。
2. 访问:类似于静态成员变量,静态成员函数可以通过类名直接调用,也可以通过类的实例调用。
3. 用途:常用于实现与具体对象无关的功能,或访问静态成员变量。

=====================================================


case1: 基本使用:

#include <iostream>

using namespace  std;

class Test{
public:
void printInfo();
};

class Myclass{
public:
    int datas;
    static int staticValue;

    void printInfo(){
    cout<<datas<<endl;
    }
    static  int getStaticValue(){
     //   datas =10; //不能 操作非静态成员变量 -->因为静态成员函数先于他们实现,这里的datas相当于还没定义
     return staticValue;
    }

};
int Myclass:: staticValue=1000;
void Test::printInfo()
{
  Myclass:: staticValue++;
  cout <<"Test 打印:"<<endl;
  cout<<Myclass::getStaticValue()<<endl; //静态成员函数能被其他成员和类调用
}


int main()
{
  Test t;

cout<<Myclass::staticValue<<endl;
cout<<Myclass::getStaticValue()<<endl;
  t.printInfo();

    return  0;
}

=============================================

case2: 基本应用


 

#include <iostream>

using namespace std;

class Myclass{
public:
    static int staticNumofInstance;

   Myclass(){
   staticNumofInstance++;
   }
   ~Myclass(){
   staticNumofInstance--;
   }

    static  int getStaticValue(){
     //   datas =10; //不能 操作非静态成员变量 -->因为静态成员函数先于他们实现,这里的datas相当于还没定义
     return staticNumofInstance;
    }

};
int Myclass::staticNumofInstance=0;

int main()
{
     Myclass m1;
    cout  <<Myclass::getStaticValue()  << endl;
    {// 规定了m2,m3的作用域,出作用域就被析构
    Myclass m2;
    cout <<Myclass::getStaticValue()  << endl;
    Myclass m3;
    cout <<Myclass::getStaticValue()  << endl;
   }
  Myclass m4;
 cout <<Myclass::getStaticValue()  << endl;
 Myclass *m5=new Myclass;
cout <<Myclass::getStaticValue()  << endl;
 delete m5;
cout <<Myclass::getStaticValue()  << endl;

    return 0;
}


  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值