C++ 各种构造函数的调用方式

news/2024/5/19 0:01:30 标签: c++, 构造函数

c++的类中有5种值得注意的基本的函数:

关于析构函数,需要注意的点在上一篇博客里面 c++: 是否会自动delete? 也提到过了,在这里暂时不多说。这篇博客主要记录这3个构造函数、1个赋值函数的调用方式,希望大家学习之后,不但知道如何调用,还可以根据一句话来判断到底调用了几次构造函数

可以通过一个例子来说明,假如我现在有一个Animal类,其中为了调试,我特意在每个构造函数中加入了输出语句,来看看调用的结果:

struct Animal {
    int age;
    string name;

    Animal() {  // 无参构造函数
        cout << "No-args Constructor" << endl;
        age = -1;
        name = "Unknown";
    }

    Animal(string name, int age) { // 有参构造函数
        cout << "Argumenting Constructor" << endl;
        this->name = name;
        this->age = age;
    }

    Animal(const Animal &other) { // 拷贝构造函数
        cout << "Copy constructor" << endl;
        this->name = other.name;
        this->age = other.age;
    }

    Animal& operator=(const Animal &other) { // 赋值函数
        cout << "Assigning" << endl;
        this->name = other.name;
        this->age = other.age;
    }

    ~Animal() {
        cout << "Destructor" << endl;
    }

    friend ostream& operator<<(ostream &out, const Animal &animal) {
        out << "Animal[" << animal.name << "]: " << "age=" << animal.age;
        return out;
    }
};

然后,以下代码会演示创建一个实例的多种方式,并且我把每一种创建方式的输出结果、以及一些思考,以注释的方式写出来:

int main() {


    //-----------------无参构造函数-----------------------

    Animal animal01; // 虽然不是很明显,但是这里不是使用指针,而是直接操作实例,所以这里实际上调用了无参构造函数
    cout << animal01 << endl;
    /*
      输出:
        No-args Constructor
        Animal[Unknown]: age=-1
    */


    Animal animal02(); // 这并不能调用无参构造函数!编译器会当作是某个函数的声明,所以下面输出的1也许是函数的地址(暂时没有深究)。
    cout << animal02 << endl;
    /*
      输出:
        1
    */


    Animal animal03 = Animal();
    // 注意,这里可能有人会认为:等号右边调用了默认构造函数,然后因为赋值给左边所以又调用了一次拷贝构造函数,就会认为这样子会白白浪费掉一个实例。
    // 实际上,根据输出可以看到,结果并不是上述那样,而是这个语句整体上只调用了一次无参构造函数。即与“Animal animal03;”效果一样。
    cout << animal03 << endl;
    /*
      输出:
        No-args Constructor
        Animal[Unknown]: age=-1
    */

    //----------------------------------------------


    //-----------------有参构造函数-----------------------  

    Animal animal001("Lion", 1);
    cout << animal001 << endl;
    /*
      输出:
        Argumenting Constructor
        Animal[Lion]: age=1
    */


    Animal animal002 = Animal("Lion", 1);
    // 这里同上面所说的,整体上仅仅调用了一次有参构造函数
    cout << animal002 << endl;
    /*
      输出:
        Argumenting Constructor
        Animal[Lion]: age=1
    */

    //----------------------------------------------


    //----------------拷贝构造函数-------------------  

    Animal animal0001 = animal001;
    // 这里要区分“拷贝构造函数”和“赋值函数”的调用时机。前者是声明和赋值在同一行,后者不是。所以这里属于拷贝构造函数
    cout << animal0001 << endl;
    /*
      输出:
        Copy constructor
        Animal[Lion]: age=1
    */  

    //-------------------------------------------


    //------------------赋值函数------------------------  

    Animal animal;
    animal = animal0001;
    // 因为这里animal的声明和赋值不在同一行,所以编译器会认为不是拷贝构造,而是一般的赋值语句。
    cout << animal << endl;
    /*
      输出:
        No-args Constructor
        Assigning
        Animal[Lion]: age=1
    */  

    //-----------------------------------

    return 0;
}
--------------------- 

转自: https://blog.csdn.net/vitalemon__/article/details/60869721


http://www.niftyadmin.cn/n/618117.html

相关文章

Python下的进程间通信-管道

背景&#xff1a; 在python下利用subprocess模块实现进程间的通信。 使用subprocess包中的函数创建子进程的时候&#xff0c;要注意: 1) 在创建子进程之后&#xff0c;父进程是否暂停&#xff0c;并等待子进程运行。 2) 函数返回什么 3) 当returncode不为0时&#xff0c;父…

C++ 拷贝构造函数与赋值函数

这里我们用类String 来介绍这两个函数&#xff1a; 拷贝构造函数是一种特殊构造函数&#xff0c;具有单个形参&#xff0c;该形参&#xff08;常用const修饰&#xff09;是对该类类型的引用。当定义一个新对象并用一个同类型的对象对它进行初始化时&#xff0c;将显式使用拷贝…

jQuery选择器之内容筛选选择器

背景&#xff1a; 基本筛选选择器针对的都是元素DOM节点&#xff0c;如果我们要通过内容来过滤&#xff0c;jQuery也提供了一组内容筛选选择器&#xff0c;当然其规则也会体现在它所包含的子元素或者文本内容上。 选择器描述$(“:contains(text)”)选择所有包含指定文本的元素…

Linux 查看与修改mtu值

MTU&#xff1a;通信术语 最大传输单元&#xff08;Maximum Transmission Unit&#xff09;是指一种通信协议的某一层上面所能通过的最大数据包大小&#xff08;以字节为单位&#xff09;。 我们在使用互联网时进行的各种网络操作&#xff0c;都是通过一个又一个“数据包”传…

并行-问题拾遗

背景: 正做并行设计的时候&#xff0c;报错&#xff1a; terminate called after throwing an instance of ‘std::bad_alloc’ 查看代码&#xff0c;发现是在做并行计算任务id&#xff08;字符串&#xff09;获取之后&#xff0c;对id做一次按照\t的分割&#xff0c;并将结…

C11标准和C++11标准

1、 C语言的C11 标准有哪些改进的地方&#xff1f;会对各类C项目起到哪些帮助&#xff1f; 1. 对齐处理操作符 alignof&#xff0c;函数 aligned_alloc()&#xff0c;以及 头文件 <stdalign.h>。 2. _Noreturn 函数标记&#xff0c;类似于 gcc 的 __attribute__((nore…

python学习笔记-半角字符和全角之间的转换

背景&#xff1a; 在做日文文本的处理时候&#xff0c;统一半全角字符。 分析&#xff1a; 说明: 全角字符unicode编码从65281~65374 &#xff08;十六进制 0xFF01 ~ 0xFF5E&#xff09; 半角字符unicode编码从33~126 &#xff08;十六进制 0x21~ 0x7E&#xff09; 特例&…

linux磁盘异常占用

背景&#xff1a; 某服务器报警&#xff0c;/根目录下磁盘超设定阈值。 分析&#xff1a; 1&#xff1a;查看磁盘占用情况 df -h 可以看出根目录已经占用了86%: 从上述信息可以看出根目录的文件系统对应的设备是/dev/sda2。 注&#xff1a;disk free(df&#xff0c;检…