Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;堆栈分配的变量未销毁(/destrocted?) 我对C++很陌生,但我认为我正确的说法是,堆栈上声明的对象在它们超出范围时会自动地被破坏/破坏。在我目前正在进行的小型项目中,情况并非如此 void MainWindow::clickTest() { FunkyNumber num = 4; FunkyNumber num2 = 6; num += num2; std::cout << num << std::endl; // This works okay! // Should be destroyed here! } void主窗口::单击测试(){ funkynumnum=4; FunkyNumber num2=6; num+=num2; std::cout_C++_Destructor - Fatal编程技术网

C++;堆栈分配的变量未销毁(/destrocted?) 我对C++很陌生,但我认为我正确的说法是,堆栈上声明的对象在它们超出范围时会自动地被破坏/破坏。在我目前正在进行的小型项目中,情况并非如此 void MainWindow::clickTest() { FunkyNumber num = 4; FunkyNumber num2 = 6; num += num2; std::cout << num << std::endl; // This works okay! // Should be destroyed here! } void主窗口::单击测试(){ funkynumnum=4; FunkyNumber num2=6; num+=num2; std::cout

C++;堆栈分配的变量未销毁(/destrocted?) 我对C++很陌生,但我认为我正确的说法是,堆栈上声明的对象在它们超出范围时会自动地被破坏/破坏。在我目前正在进行的小型项目中,情况并非如此 void MainWindow::clickTest() { FunkyNumber num = 4; FunkyNumber num2 = 6; num += num2; std::cout << num << std::endl; // This works okay! // Should be destroyed here! } void主窗口::单击测试(){ funkynumnum=4; FunkyNumber num2=6; num+=num2; std::cout,c++,destructor,C++,Destructor,这是否在Windows GUI应用程序(具有WinMain入口点的Windows应用程序)中 如果是,则从命令行运行标准输出时不会自动显示。我不确定原因,但IIRC正在运行: myapp | cat 应使标准输出设置正确。我无法重现该行为 #include<iostream> struct FunkyNumber{ int m_intValue; FunkyNumber::FunkyNumber(int num) : m_intValue(num)

这是否在Windows GUI应用程序(具有
WinMain
入口点的Windows应用程序)中

如果是,则从命令行运行标准输出时不会自动显示。我不确定原因,但IIRC正在运行:

myapp | cat

应使标准输出设置正确。

我无法重现该行为

#include<iostream>

struct FunkyNumber{
    int m_intValue;
    FunkyNumber::FunkyNumber(int num)
        : m_intValue(num) {
            std::cout << "made a funkynumber " << num << std::endl;
    }

    FunkyNumber::~FunkyNumber() {
        std::cout << "goodbye cruel world! (" << m_intValue << ")" << std::endl;
    }

    int FunkyNumber::intValue() const {
        return m_intValue;
    }

    void FunkyNumber::operator+=(const FunkyNumber &other) {
        m_intValue += other.intValue();
    }

    void FunkyNumber::operator=(const FunkyNumber &other) {
        m_intValue = other.intValue();
    }

    bool FunkyNumber::operator==(const FunkyNumber &other) {
        return other.intValue() == m_intValue;
    }
};

std::ostream &operator<<(std::ostream &outStream, const FunkyNumber &num) {
    outStream << "FunkyNumber (" << num.intValue() << ")";

    return outStream;
}

void call(){
    FunkyNumber num = 4;
    FunkyNumber num2 = 6;

    num += num2;
    std::cout << num << std::endl; // This works okay!

    // Should be destroyed here!
}

int main(int argc, char **argv){
    call();
    std::cout << "call ended" << std::endl;
}
#包括
结构FunkyNumber{
int m_int值;
FunkyNumber::FunkyNumber(int num)
:m_intValue(num){

std::如果对象正在被销毁,那么无论出于什么原因,您都看不到它。您可以将整个FunkyNumber类添加到post中吗?您必须看到更多的代码——可能很简单,因为您的析构函数实现没有被使用,默认值为(空)正在调用析构函数。请使用调试器并在析构函数中设置一个断点。是否在控制台/终端中运行?您会非常失望,但我刚刚重新编译了项目(清理和构建),现在它可以正常工作了!如图所示。。。
myapp | cat
#include<iostream>

struct FunkyNumber{
    int m_intValue;
    FunkyNumber::FunkyNumber(int num)
        : m_intValue(num) {
            std::cout << "made a funkynumber " << num << std::endl;
    }

    FunkyNumber::~FunkyNumber() {
        std::cout << "goodbye cruel world! (" << m_intValue << ")" << std::endl;
    }

    int FunkyNumber::intValue() const {
        return m_intValue;
    }

    void FunkyNumber::operator+=(const FunkyNumber &other) {
        m_intValue += other.intValue();
    }

    void FunkyNumber::operator=(const FunkyNumber &other) {
        m_intValue = other.intValue();
    }

    bool FunkyNumber::operator==(const FunkyNumber &other) {
        return other.intValue() == m_intValue;
    }
};

std::ostream &operator<<(std::ostream &outStream, const FunkyNumber &num) {
    outStream << "FunkyNumber (" << num.intValue() << ")";

    return outStream;
}

void call(){
    FunkyNumber num = 4;
    FunkyNumber num2 = 6;

    num += num2;
    std::cout << num << std::endl; // This works okay!

    // Should be destroyed here!
}

int main(int argc, char **argv){
    call();
    std::cout << "call ended" << std::endl;
}