继续实现Vector类
发布日期:2022-02-07 06:39:45 浏览次数:3 分类:技术文章

本文共 3657 字,大约阅读时间需要 12 分钟。

继续实现Vector类

大纲

1. 尝试实现比较不同Vector对象的成员函数
2. this指针
3. 初涉运算符重载
4. 初涉友元函数

这里呢,我想实现一个这样的成员函数

能够让Vector类的对象和另一个Vector对象进行比较
然后返回长度较长的对象

尝试实现

函数原型

Vector max(Vector tv);
函数定义

Vector Vector::max(Vector tv){    if( (x*x + y*y) < (tv.x*tv.x + tv.y*tv.y))        return tv;    else        return ??;}

当我想返回自己的时候,遇到问题了

怎么返回自己呢??

this指针

类内部,有一个叫做this指针的东西

它表示的是指向被调用对象本身
比如:
我可以这样修改这一个函数

Vector Vector::max(Vector tv){    if( (x*x + y*y) < (tv.x*tv.x + tv.y*tv.y))        return tv;    else        return *this;}

通过return*this,就达到了返回自己的目的

此后,我们便可以这样调用

Vector v1(2,3);Vector v2(3,4);Vector m;m = v1.max(v2);

这样子,便可以找到v1和v2中的较长矢量

并返回对象赋值给max

同时,在类内部

还可以这样子使用this调用类的内部成员
this->x
this->y

实际上,在类的内部定义函数的时候

都会隐式的传递this指针
(就像函数传递参数一样)
通过this指针,可以访问自身的成员
也可以实现返回自身

const成员函数

显式的传递参数,我们可以加上const来防止被改动

但是在类成员函数中
类自身的传递是隐式的(即this指针)
我们有办法给this指针加上const
防止函数修改内部成员吗?
这里拿show()函数做例子
show()仅仅是作为输出而不用于修改
为了防止误操作,我可以给show()函数加上const
防止自己的误操作会修改类的内部成员

// 函数原型void show() const;//函数定义void Vector::show() const{    using namespace std;    cout << x << "," << y << endl;    return ;}

这里的const,放在了函数的末尾

我的理解是隐式传递的this指针没位置放const
然后不得已在后面找了个没东西放的放const
……
当然了
其实这个函数还可以这样写

// 函数原型void show() const;//函数定义void Vector::show() const{    using namespace std;    cout << this->x << "," << this->y << endl;    return ;    //this是指向当前调用对象的指针    //所以当然可以这样子访问内部成员}

上面的写法是和之前的写法等价的!!

初涉运算符重载

这样子找v1,v2中的最大值

寻找最大值的语句可能有点奇怪
m = v1.max(v2);
我能不能这样子呢?

if (v1 < v2)    max = v2;else    max = v1;

对于 < 运算符而言,它能够处理很多种数据类型

能够处理int,double,char……
现在我们有了一种新的类型Vector
这种类型,能够这样比较吗?
需要自己定义适用于这个类型的运算符
下面先放一个运算符重载的例子

//函数原型:bool operator< (vector tv);//函数定义bool Vector::operator<(vector tv){    if ( (x*x + y*y) < (tv.x*tv.x + tv.y*tv.y) )        return true;    else        return false;}

这里,相当于定义了名字为operator<的一个函数

然后运算符< 的左边的操作数是被调用对象
< 右边的是括号内的tv对象
也就是说
(v1 < v2)
相当于这样子的函数调用
v1.operator<(v2)

初涉友元函数

原来寻找最大值的语句

m = v1.max(v2);
我是否可以这样子?
看起来比较正常?
m = max(v1,v2);

在这里

我们也许会尝试这样

//函数原型Vector max(Vector v1, Vector v2);//函数定义Vector max(Vector v1, Vector v2){    if ( (v1.x*v1.x + v1.y*v1.y) >             (v2.x*v2.x + v2.y*v2.y) )        return v1;    else        return v2;}

可是!

这个函数在类的外部
不能够访问类的私有成员
而x,y,都是这个类的私有成员
怎么办?
这个时候,友元函数就用处大了
友元函数
提供一个非类成员函数访问类的私有成员的机会
关键字:friend
所以有如下函数声明与定义

//函数原型要放到类定义里面//函数原型friend Vector max(Vector v1, Vector v2);//函数定义Vector max(Vector v1, Vector v2){    if ( (v1.x*v1.x + v1.y*v1.y) >             (v2.x*v2.x + v2.y*v2.y) )        return v1;    else        return v2;}

注意:max函数不是类成员函数,

所以函数定义的时候不需要类名+作用域解析运算符
函数的声明放在类定义中,且加上关键词friend
是给予了函数max访问私有成员的权限

最后

可能还不知道代码怎么放

下面还是一个多文件的实例
1. Vector.h
2. Vector.cpp
3. test.cpp

// Vector.h#ifdef VECTOR_H#define VECTOR_Hclass Vector{private:    int x;    int y;public:    Vector() {};//默认构造函数    Vector(int a, int b);//自定义构造函数    ~Vector(){};//析构函数    void show() const;    Vector max(Vector tv);    bool operator< (vector tv);    friend Vector max(Vector v1, Vector v2);};#endif
// Vector.cpp#include "Vector.h"#include 
Vector::Vector(int a, int b){ x = a; y = b;}void Vector::show() const{ using namespace std; cout << x << "," << y << endl; return ;}Vector Vector::max(Vector tv){ if( (x*x + y*y) < (tv.x*tv.x + tv.y*tv.y)) return tv; else return *this;}bool Vector::operator<(vector tv){ if ( (x*x + y*y) < (tv.x*tv.x + tv.y*tv.y) ) return true; else return false;}Vector max(Vector v1, Vector v2){ if ( (v1.x*v1.x + v1.y*v1.y) > (v2.x*v2.x + v2.y*v2.y) ) return v1; else return v2;}
//test.cpp#include "Vector.h"#include 
int main(){ .... return 0;}

这里用了三种方式找到两个类对象的最大值

主要是想让大家了解到三种技术
1. this指针的使用
2. 运算符重载
3. 友元函数

转载地址:https://blog.csdn.net/wyfwyf12321/article/details/54744377 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:浅谈构造函数和析构函数
下一篇:浅谈运算符重载与友元函数

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月13日 19时30分41秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章