c++ - 应该是一个虚拟析构函数?但是怎么办?

标签 c++ linked-list destructor

一个将电话公司的消费者数据存储在链表中的程序。最后它显示每个人的账单。我有以下代码:

class BaseTypeOfContract
{
private:
   int minutePrice;
   int SMSPrice;
public:
    void setminutePrice(int x) { minutePrice = x; }
    void setSMSPrice(int x) { SMSPrice = x; }

    virtual int calculateBill(int talkedMinutes, int sentSMS) = 0;

    int getminutePrice() const { return minutePrice; }
    int getSMSPrice() const { return SMSPrice; }

};


class SMSBaseType : public BaseTypeOfContract
{
private:
   int freeSMS;
public:
   SMSBaseType(int minutePrice, int SMSPrice, int freeSMS)
   {
       setminutePrice(minutePrice);
       setSMSPrice(SMSPrice);
       setfreeSMS(freeSMS);
   }

public:

    void setfreeSMS(int free) { this->freeSMS = free; }

    virtual int calculateBill(int talkedMinutes, int sentSMS)
    {
        int billedSMS = (freeSMS > sentSMS) ? 0 : sentSMS - freeSMS;
        return talkedMinutes * getminutePrice() + billedSMS * getSMSPrice();
    }
};

class Base : public BaseTypeOfContract
{
public:
    Base()
    {
        setminutePrice(30);
        setSMSPrice(10);
    }
    virtual int calculateBill(int talkedMinutes, int sentSMS) { return talkedMinutes * getminutePrice() + sentSMS * getSMSPrice();}
};

class SMSMax : public SMSBaseType
{
public:
   SMSMax() : SMSBaseType(20, 5, 150) {}
};

class MobiNET: public SMSBaseType
{
public:
   MobiNET() : SMSBaseType(10, 15, 25) {}
};

客户的类(class):

class Client
{
public:
    std::string name;
    std::string phoneNumber;
    BaseTypeOfContract* typeOfContract;
    int talkedMinutes;
    int sentSMS;
    Client *next;
public:
    Client(){}
    Client(std::string n, std::string p, int bp, int ks) : name(n), phoneNumber(p), talkedMinutes(bp), sentSMS(ks) {}


    void preSetPlan(std::string s)
    {
        if (s == "MobiNET")
            this->typeOfContract = new MobiNET();
            else if (s == "SMSMax")
                this->typeOfContract = new SMSMax();
                else this->typeOfContract = new Base();
    }

    std::string getname() const { return name; }
    std::string getphoneNumber() const { return phoneNumber; }

    void setname(std::string n) { name = n; }
    void setphoneNumber(std::string pn) { phoneNumber = pn; }
    void settalkedMinutes(int bp) { talkedMinutes = bp; }
    void setsentSMS(int SSMS) { sentSMS = SSMS; }

    int getBill() const { return this->typeOfContract->calculateBill(talkedMinutes, sentSMS); }

};

我从 2 个文件中读取数据。第一个文件包含姓名、电话号码、契约(Contract)类型。第二个文件包含电话号码、通话时间和发送的短信。

Client* file_read_in()
{
    std::ifstream ClientData;
    ClientData.open("clients.txt");

    Client *first = new Client;
    first = NULL;

    while (!ClientData.eof())
    {
        std::string name, phoneNumber, typeOfContract;
        ClientData >> name;
        ClientData >> phoneNumber;
        ClientData >> typeOfContract;

        std::ifstream ClientTalkedSent;
        ClientTalkedSent.open("used.txt");

        while(!ClientTalkedSent.eof())
        {
            std::string phoneNumber2;
            ClientTalkedSent >> phoneNumber2;

            if (phoneNumber2 == phoneNumber)
            {
                int talkedMinutes, sentSMS;
                ClientTalkedSent >> talkedMinutes;
                ClientTalkedSent >> sentSMS;

                Client* tmp = new Client(name, phoneNumber, talkedMinutes, sentSMS);
                tmp->preSetPlan(typeOfContract);
                tmp->next = NULL;

                if (first == NULL)
                {
                    first = tmp;
                }
                else
                {
                    Client *cond = first;
                    while (cond->next != NULL) cond = cond->next;
                    cond->next = tmp;
                }

            }
        }

        ClientTalkedSent.close();
    }
    ClientData.close();

    return first;
}

还有主要的:

int main()
{
    Client* first = file_read_in();

    while(first != NULL)
    {
        std::cout << first->getname() << " " << first->getphoneNumber() << " " << first->getBill() << std::endl;
        first = first->next;
    }

    return 0;
}

我的问题是我应该释放分配的内存,但我知道如何释放。哪个类的析构函数应该做肮脏的工作。如果有人可以使用我的代码来展示“析构函数继承”的工作原理,我将不胜感激。

抱歉我的英语不好,感谢您的帮助。这个网站帮助了我很多次,但是对于这个问题我没有找到解决方案。

最佳答案

如果你有一个指针BaseTypeOfContract* typeOfContract;用来指向不同的派生类,然后BaseTypeOfContract delete typeOfContract 需要一个虚拟析构函数去工作。

作为Client似乎创建了指向的对象,它也应该负责清理它们。通过使用 delete typeOfContract;在其析构函数中,或通过存储智能指针来自动完成工作。

另一部分是每个Client存储指向下一个 Client 的指针.这似乎不是最好的设计。在现实生活中,根本不像每个人都知道谁是同一家商店的下一个购买手机的人。 :-)

使用容器会更好,例如 std::vector<Client> ,这也将处理 Client 的生命周期对象。

关于c++ - 应该是一个虚拟析构函数?但是怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50315230/

相关文章:

c - 链表头始终设置为 NULL

php - 为什么 phpunit 不在模拟类中运行 __destruct() 以及如何强制运行它?

c++ - CRITICAL_SECTION 并避免包含 windows.h?

c++ - memmove 不动

Java:高效插入LinkedList

c++ - 标量删除析构函数问题

c++ - 删除在析构函数中不起作用的字符指针

c++ - 如何在VS2015中使用VisualGDB嵌入式C++重构特性

c++ - 通过 boost::function_base 调用 boost::function

c - 在C中创建链表时程序崩溃