简体   繁体   中英

C++ stack allocated variable not destructed (/destroyed?)

I'm quite new to C++ but I think I'm right in that saying objects declared on the stack should automagically be destructed/destroyed when they go out of scope? In the mini-project I'm working with at the moment, this isn't the case.

void MainWindow::clickTest() {
    FunkyNumber num = 4;
    FunkyNumber num2 = 6;

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

    // Should be destroyed here!
}

My destructor should do this:

virtual FunkyNumber::~FunkyNumber() {
    std::cout << "goodbye cruel world! (" << m_intValue << ")" << std::endl;
    // m_intValue is just the int value of this "FunkyNumber"
}

But nothing comes out into standard out!

Should probably mention I'm using Qt - but this is just a plain C++ class and so this shouldn't really matter from what I can tell...

EDIT: funkynumber.cpp:

#include "funkynumber.h"

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;
}

Is this in a Windows GUI application (Windows application with a WinMain entry point)?

If it is, the standard output does not get displayed automatically when running it from the command line. I am not sure why this is, but IIRC running:

myapp | cat

should cause the standard output to be setup correctly.

I can not reproduce the behavior.

#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;
}

This works fine. The reason why people promote SSCCE is not only to make it easier to help you but also because it can help you find where the issue is yourself(which is clearly not in the code you posted).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM