Trying to run this code, to see how anonymous object ctor/dctor are called in a single expression:
#include <math.h>
#include <iostream>
using namespace std;
class Test {
public:
int mA = 0;
Test() {
mA = 1000;
cout << "ctor test" << endl;
}
~Test() {
cout << "dctor test" << endl;
}
};
class MainPanel {
public:
Test mTest;
MainPanel(Test *test) : mTest(*test) {
cout << "ctor main" << endl;
}
~MainPanel() {
cout << "dctor main" << endl;
}
};
Test Crea() {
cout << "crea" << endl;
return Test();
}
int main()
{
cout << "init " << endl;
MainPanel mainPanel = MainPanel(&Crea());
cout << mainPanel.mTest.mA << endl;
cout << "end " << endl;
}
But (for example here with on g++
) it deny this code: error: taking address of temporary [-fpermissive]
Instead, on msvc
its seems permitted.
Why this could be "dangerous" to prevent it in first place?