C++ | 概念 | 引用

  看了几篇C++的代码例子,发现理解起来有点儿费劲,慢下来细细品味发现有个“符号”反复出现在函数参数,函数返回值,觉得有必要查书深入理解一下。

  引用 是 已定义的变量的别名。它的主要用途是用作函数的形参。通过将引用变量用作参数,函数将使用原始数据,而不是其副本。对应C语言函数中的地址传递与值传递。

int rats;
int &rodents = rats;		//基本用法
void swap_a(int a, int b)
{
    int temp;
    temp = a;
    a = b;
    b = temp;
}
//这个在C语言里是个知识点:由于传入到函数内部的a 和 b其实会是实参的副本,所以实际两个实参对应的数值并没有发生交换。
//对于C语言来说,通过使用指针来传递
void swap_b(int *a, int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}
//对于曾经的我来说,指针的使用看起来阅读性不是那么好
//如果使用新的概念:引用的话,看起来就好一些了,与指针实现的功能是一样的
void swap_c(int &a, int &b)
{
    int temp;
    temp = a;
    a = b;
    b = temp;
}

引用 跟 常指针 int *const ptr 几乎是等价的。

引用与指针

  引用 和 指针有一定的相似,引入引用主要是为了用于结构体和类对象这些类型,而不是基本的内置类型。

  • 如果数据对象是数组,则只能使用指针。

  • 如果数据对象是结构体,则可以使用引用 或 指针。

  • 如果数据对象是类对象,则使用引用。

引用与函数重载

  函数重载是指有多个同名的函数,因此对名称进行了重载。

  函数重载的要求是它们的参数数目、类型、排列顺序不同。

//我看到的C++文章中是这么写的
int add_one(int *a)
{
    *a = *a + 1;
    return *a;
}

int add_one_ref(int &b)
{
    b = b+1;
    return b;
}
//联想到函数重载的概念,我试着改为int add_one(int &b) 进行调用,结果是:编译报错。

引用与拷贝构造函数

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Person {
private:
    char *name;
    int age;
    char *work;

public:

    Person() {//cout <<"Pserson()"<<endl;
        name = NULL;
        work = NULL;
    }
    Person(char *name, int age, char *work = "none") 
    {
        cout <<"Pserson(char*, int)"<<endl;
        this->age = age;

        this->name = new char[strlen(name) + 1];
        strcpy(this->name, name);

        this->work = new char[strlen(work) + 1];
        strcpy(this->work, work);
    }

    Person(Person &per) 
    {
        cout <<"Pserson(Person &per)"<<endl;
        this->age = per.age;

        this->name = new char[strlen(per.name) + 1];
        strcpy(this->name, per.name);

        this->work = new char[strlen(per.work) + 1];
        strcpy(this->work, per.work);
    }

    ~Person()
    {
        cout << "~Person()"<<endl;
        if (this->name) {
            cout << "name = "<<name<<endl;
            delete this->name;
        }
        if (this->work) {
            cout << "work = "<<work<<endl;
            delete this->work;
        }
    }

    void printInfo(void)
    {
        //printf("name = %s, age = %d, work = %s\n", name, age, work); 
        cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
    }
};

int main(int argc, char **argv)
{
    Person per("zhangsan", 18);
    Person per2(per);

    per2.printInfo();

    return 0;
}

关于拷贝构造函数

  构造函数为类的一种特殊的成员函数,在每次创建新的对象时执行,构造函数可以用于为某些成员变量设置初始值。

  拷贝构造函数为构造函数的一种,拷贝已经创建的对象的初始值。

Person(Person &per) 
{
    cout <<"Pserson(Person &per)"<<endl;
    this->age = per.age;

    this->name = new char[strlen(per.name) + 1];
    strcpy(this->name, per.name);

    this->work = new char[strlen(per.work) + 1];
    strcpy(this->work, per.work);
}

  代码来源参考了这篇文章:适合具备C语言基础的C++教程

  还搜集到一篇关于引用的文章:C++的引用有点儿意思

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值