What happens to the return value if I don't store it anywhere?

前端 未结 2 1636
我在风中等你
我在风中等你 2020-12-10 16:25

In the small sample below:

#include
using namespace std;

int z(){
    return 5 + 10; // returns 15
}

int main(){
    z(); // what happens          


        
2条回答
  •  盖世英雄少女心
    2020-12-10 16:50

    In general: when a function returns a non-void value and the value does not get stored anywhere, the value is destroyed.

    Specifically: natural datatypes, like ints and doubles, or pointers, don't have an explicit destructor, so nothing really happens. The returned value simply gets ignored.

    If a function returns a class instance, the class instance gets destroyed, which results in an invocation of the class's defined destructor, or a default destructor.

提交回复
热议问题