#include <iostream>
using namespace std;
class A {
public:
A() {
cout << "Default Ctor" << endl;
}
};
int main() {
A(); // <------- Problem
return 0;
}
It shows Default Ctor on console. My questions
- Is is valid?
- If so, how did it instantiate since I didn’t use new or any object?
You are creating a new object with
A().Is is valid?
Yes it is.
If so, how did it instantiate since I didn’t use new or any object?
newsimply creates the object in dynamic memory. You’re creating the object in automatic memory. Also, just because you didn’t give the object a name, doesn’t mean it isn’t created.Think of this:
Leaving optimizations aside Did
foo()actually return1? Yes it did. Just that you’re not using it.EDIT:
Let’s break it down a bit: