求助:c++怎么为必要的类添加合适的拷贝构造函数、赋值操作符?

//chair.h

 #ifndef SHAPE__H
#define SHAPE__H

#include <string>
#include <vector>
#include <iostream>

static int gid=0;
int getuid(){
    return ++gid;
}

class Seat{
private:
    int id;
public:
    Seat(){ id=getuid();std::cout<<"Seat() "<<id<<" called\n";}
    ~Seat(){ std::cout<<"~Seat() "<<id<<" called\n";}
    int getid() const {return id;}
};

class Leg{
private:
    int id;
public:
    Leg(){ id=getuid();std::cout<<"Leg() "<<id<<" called\n";}
    ~Leg(){ std::cout<<"~Leg() "<<id<<" called\n";} 
};

class Chair{
private:
    int id;
public:
    Chair(){ id=getuid();std::cout<<"Chair() "<<id<<" called\n";}
    ~Chair(){ std::cout<<"~Chair() "<<id<<" called\n";}
    void setSeat(Seat& s){ seat = s;}
    void setLeg(Leg& l){ legs.push_back(l);}
private:
    Seat seat;
    std::vector<Leg> legs;
};

class Swivel{
private:
    int id;
public:
    Swivel(){ id=getuid();std::cout<<"Swivel() "<<id<<" called\n";}
    ~Swivel(){ std::cout<<"~Swivel() "<<id<<" called\n";}   
};

class OfficeChair : public Chair{
private:
    int id;
public: 
    OfficeChair(){ id=getuid();std::cout<<"OfficeChair() "<<id<<" called\n";}
    ~OfficeChair(){ std::cout<<"~OfficeChair() "<<id<<"  called\n";}
    void setSwivel(Swivel& s) { swivel = s;}
private:
    Swivel swivel;
};

#endif

//chair.cpp

 #include "chair.h"
#include <iostream>
#include <string>
#include<iomanip>

using namespace std;

void test_1(){
    cout<<"test_1 begin\n";
    {
        Seat s;
        Leg L1;
        Leg L2;
        Leg L3;
        Leg L4;
        Swivel sw;
        OfficeChair oc;
        oc.setSeat(s);
        oc.setLeg(L1);
        oc.setLeg(L2);
        oc.setLeg(L3);
        oc.setLeg(L4);
        oc.setSwivel(sw);
    }
    cout<<"test_1 end\n";
}
int main(){
    test_1();
}

测试输出:
/*
test_1 modified begin
Seat() 1 called
Leg() 2 called
Leg() 3 called
Leg() 4 called
Leg() 5 called
Swivel() 6 called
Seat() 7 called
Chair() 8 called
Swivel() 9 called
OfficeChair() 10 called
Seat() copyed 11 1 called
Leg() copyed 12 2 called
Leg() copyed 13 12 called
Leg() copyed 14 3 called
~Leg() 12 called
Leg() copyed 15 13 called
Leg() copyed 16 14 called
Leg() copyed 17 4 called
~Leg() 13 called
~Leg() 14 called
Leg() copyed 18 15 called
Leg() copyed 19 16 called
Leg() copyed 20 17 called
Leg() copyed 21 5 called
~Leg() 15 called
~Leg() 16 called
~Leg() 17 called
Swivel() copyed 22 6 called
~OfficeChair() 10 called
~Swivel() 22 called
~Chair() 8 called
~Leg() 18 called
~Leg() 19 called
~Leg() 20 called
~Leg() 21 called
~Seat() 11 called
~Swivel() 6 called
~Leg() 5 called
~Leg() 4 called
~Leg() 3 called
~Leg() 2 called
~Seat() 1 called
test_1 end
*/

1个回答

举个例子,当你的类里面使用的是其他的类当子类,或者你的类里面使用其他的结构体,系统缺省的只有基本数据结构的数值的赋值,还有就是你的类里面使用的是指针类,当你需要赋值的时候,会把同一个地址赋值给对象(A->B),这样,当 你赋值的对像析构的时候,会把那个地址的值析构了(~B),那你原来的类就会析构报错(~A),所以要重写赋值和构造函数

play_841266670
diskings 回复ShiLuoTianKongch: 我运行你的代码了,也发现你的问题,就是你赋值的会变成全局变量gid,你在学类的话,指导书会有创建operate + 的函数,然后就是你的跟踪可以像我这样写L1.id,L2.id,如果你没指导书,在私信我吧,我晚点再写给你
接近 3 年之前 回复
ShiLuoTianKongch
ShiLuoTianKongch 可以具体到这个例子么,因为我这个如果不写的话,编译器默认就把跟踪用的id覆盖掉了,不知道应该怎么重写
接近 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问