samedi 23 février 2019

My default constructor is not being called even though I have defined one

To my understanding of c++ I always thought I should be able to do the following and call the default constructor in this manner:

#include <iostream>

void do_some_work() {
    std::cout << "Doing some work" << std::endl;
}

void do_something_else() {
    std::cout << "Doing something else." << std::endl;
}


class background_task
{
public:
    background_task()
    {
        std::cout << "Calling the constructor!" << std::endl;
    }
};

int main()
{
    background_task f();
    getchar();
    return 0;
}

After executing the program above I see that the default constructor I have provided and instead it is only blank and no message gets outputted on the screen. I know that I am supposed to call it in this way:

background_task f;

And when I do it calls normally my default constructor and I see the printing in the screen. I wonder why in the first case it doesn't appear anything, is it not legal that statement if not why doesn't the crash happen?

Aucun commentaire:

Enregistrer un commentaire