gpt4 book ai didi

c++ - 如何从析构函数中调用成员函数

转载 作者:搜寻专家 更新时间:2023-10-31 02:16:59 43 4
gpt4 key购买 nike

我正在为一个 C++ 项目工作,但我想知道如何从析构函数中的类/结构调用成员函数。我项目的其余部分进展顺利,所以我只需要知道它就可以完成:)

    ~Drink()
{
cout << "hi";
cout << "I want to know how to summon a member function from a destructor.";
}

int Drink::dailyReport()
{
cout << "This is the member function I want to call from the destructor.";
cout << "How would I call this function from the destructor?"
}

正确的语法和所有将不胜感激!如何从析构函数调用成员函数的示例会很可爱!

最佳答案

这里的问题是您没有为析构函数添加前缀 Drink::

由于析构函数不知道它应该与哪个类相关联(假设它是在类声明之外定义的),它甚至不知道它有一个成员函数要调用。

尝试将您的代码重写为:

Drink::~Drink() // this is where your problem is
{
cout << "hi";
cout << "I want to know how to summon a member function from a destructor.";
dailyReport(); // call the function like this
}

int Drink::dailyReport()
{
cout << "This is the member function I want to call from the destructor.";
cout << "How would I call this function from the destructor?"
}

关于c++ - 如何从析构函数中调用成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36256319/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com