C++:对象数组与this指针

C++:对象数组与this指针

标签(空格分隔): c++

版本:2
作者:陈小默
声明:禁止商用,禁止转载

该文章仅被发布于作业部落(原)CSDN


声明对象数组

由于在C++中我们可以直接操作堆和栈,所以在声明时我们也需要选择数组的创建方式。有一种最显而易见的方法去判断变量的存储状态,就是看对象创建时有没有用new关键字。
1,栈数组栈对象
顾名思义就是数组和对象都在当前栈中分配,带来的好处就是对象数组的生命周期有限,我们不需要去手动释放栈中的内存,但是缺点就是创建的对象我们只能在当前栈帧中使用,不能直接将引用返回到其他栈帧。

    Student stu[3]={
        Student("Jack",90,97,89),
        Student("Sue",92,95,100),
        Student("Sam",89,98,99)
    };

当stu的生命周期终结时,其中所有对象的析构函数都将会被调用。

2,栈数组和堆对象
有时我们需要将分散在堆中数据进行汇总操作,这时就可以使用这种方式创建一个指针数组

    Student *students[3]={
        new Student("Jack",90,97,89),
        new Student("Sue",92,95,100),
        new Student("Sam",89,98,99)
    };

当students生命周期结束时,这个数组将会被销毁并清空其中的引用。但其中的Student对象仍在堆中存在,并且此时没有任何指针指向这些内存,长此以往将会导致内存泄漏。

this指针

在C++中,this是一个指针常量,指向当前正在调用这个方法的对象。

stu[1].show();

如果在show方法中使用了this指针,则这个this指针就代指stu[1]对象。

代码示例

我们以一个学生列表查找最好成绩的例子来演示本节内容,首先仍然是创建一个头文件student.h

//student.h -- Student class interface
//version 00
#ifndef STUDENT_H_
#define STUDENT_H_

#include <string>

class Student{ //class declaration
private:
    std::string name;
    int ch;
    int en;
    int math;
    float average;
    void count(){
        average = (ch + en + math+0.0F)/3;
    }
public:
    Student();
    Student(std::string name,int chScore,int enScore,int mathScore);
    ~Student();
    Student & max1(Student & stu);
    Student * max2(Student * stu);
    void show();
};

#endif

然后我们去实现student.cpp,注意,这里我们提供了为上述两种数组的创建方式提供了比较的办法,在max中,我们向一个对象传递了另一个对象的引用,当我们进行判断之后选择是返回自身对象的引用(this)或者是参数对象的引用。

//student.cpp -- implementing the Student class
//version 00
#include "stdafx.h"
#include "student.h"

Student::Student(){}
Student::Student(std::string n,int chScore,int enScore,int mathScore):name(n),ch(chScore),en(enScore),math(mathScore){
    count();
}
Student::~Student(){
    cout<<"再见! "<<name<<"."<<endl;
}
Student & Student::max1(Student & stu){
    float a1= (*this).average;
    float a2=stu.average;
    return a1>a2?*this:stu;
}
Student* Student::max2(Student * stu){
    float a1= this->average;
    float a2= stu->average;
    return a1>a2?this:stu;
}
void Student::show(){
    Student::count();
    ios_base::fmtflags orig = cout.setf(ios_base::fixed,ios_base::floatfield);
    std::streamsize prec = cout.precision(1);
    cout<<name<<" 同学的语文成绩为"<<ch<<"分,数学成绩为"<<math<<"分,英语成绩为"<<en<<"分,平均成绩"<<average<<"分"<<endl;
    cout.setf(orig,ios_base::floatfield);
}

我们在main方法中实现代码

//Visual Studio 2010 -- main program

#include "stdafx.h"
#include "student.h"
void fun1();
void fun2();

int _tmain(int argc, _TCHAR* argv[]){
    fun1();
    cout<<endl;
    fun2();
    return 0;
}
void fun1(){
    Student students[3]={
        Student("Jack",90,97,89),
        Student("Sue",92,95,100),
        Student("Sam",89,98,99)
    };
    Student max = students[0];

    for(int i=1;i<3;i++){
        max = max.max1(students[i]);
    }
    max.show();
}
void fun2(){
    Student *students[3]={
        new Student("Jack",90,97,89),
        new Student("Sue",92,95,100),
        new Student("Sam",89,98,99)
    };
    Student * max = * students;

    for(int i=1;i<3;i++){
        max = max->max2(*(students+i));
    }
    max->show();

    for(int i=0;i<3;i++){
        delete students[i];
    }
}

以下是代码运行结果

再见! Jack.
再见! Sue.
再见! Sam.
Sue 同学的语文成绩为92分,数学成绩为100分,英语成绩为95分,平均成绩95.7分
再见! Sue.
再见! Sam.
再见! Sue.
再见! Jack.

Sue 同学的语文成绩为92分,数学成绩为100分,英语成绩为95分,平均成绩95.7分
再见! Jack.
再见! Sue.
再见! Sam.
请按任意键继续…

从结果中我们可以看出,当我们使用fun1()中栈对象的时候创建并销毁了3个临时对象。对于大量的数据,创建对象和内容复制的时间浪费是相当大的。对于fun2()来说当我们创建对象成功后,并不需要去赋值内容,而仅仅是将引用传递给数组保存即可。但是要注意的是,堆中分配的数据一定要回收内存。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值