我的复制构造函数没有被调用

my copy constructer is not called

这是主文件

#include <bits/stdc++.h>
#include "animal.h"
#include<sstream>

using namespace  std ;

int main(){

    Animal elephant("ele" ,12);
    Animal cow("cow" ,22) ;
    cow = elephant ;
    cow.a[0]=5 ;
    return 0 ;
}

这是Animal.h文件

#ifndef ANIMAL_H
#define ANIMAL_H

#include<iostream>
#include<string>
using namespace std ;
class Animal{
    string name ;
    int age  ;

public :
    int a[] ;
    Animal(string name , int age ):name(name) ,age(age) {}
    Animal(const Animal & other);


};

#endif // ANIMAL_H

这是Animal.cpp

#include"animal.h"
#include<iostream>
using namespace std ;
Animal::Animal(const Animal & other){
    cout<<"copy constructor is called"<<endl ;
    this->age=other.age ;
    this->name = other.name ;
}

我无法调用复制构造函数??代码有什么问题。我已经给出了所有文件的名称和代码。

之后
Animal cow("cow" ,22) ;

cow 存在。它已经建成。无法再次构造,所以

cow = elephant ;

是赋值并调用赋值运算符 operator=。让我们添加一个赋值运算符

Animal & Animal::operator=(const Animal & other){
    cout<<"Assignment operator is called"<<endl ;
    this->age=other.age ;
    this->name = other.name ;
}

Animal看看会发生什么:https://ideone.com/WlLTUa

Animal cow = elephant

会调用复制构造函数(示例:https://ideone.com/sBdA1d

另请阅读 Copy Elision 以了解另一个可以导致 "Dude, Where's my copy?" 类型问题的技巧。