C ++ Seltsames Konstruktorverhalten
Kann mir jemand den Unterschied erklären zwischenKomplex a; undKomplex b ();?
#include<iostream>
class Complex
{
public:
Complex()
{
std::cout << "Complex Constructor 1" << std::endl;
}
Complex(float re, float im)
{
std::cout << "Complex Constructor 2" << std::endl;
}
~Complex()
{
std::cout << "Complex Destructor" << std::endl;
}
};
int main()
{
Complex a;
std::cout << "--------------------------" << std::endl;
Complex b();
std::cout << "--------------------------" << std::endl;
Complex c(0,0);
std::cout << "--------------------------" << std::endl;
return 0;
}
Ausgabe:
Complex Constructor 1
--------------------------
--------------------------
Complex Constructor 2
--------------------------
Complex Destructor
Complex Destructor
Wie du siehst,Komplex a; ruft seinen Standardkonstruktor auf,Komplex b (); nicht undKomplex c (0,0); ruft einen überladenen Konstruktor auf.
Was geht hier vor sich? Ich dachte, dassKomplex b (); würde eine Stack-Variable erstellen und ihren Standardkonstruktor aufrufen, um sie zu initialisieren?