Is there any difference between C c; and C c = C();?

Viewed 164
#include<iostream>
using namespace std;

class C{
private:
    int value;
public:
    C(){
        value = 0;
        cout<<"default constructor"<<endl;
    }
    C(const C& c){
        value = c.value;
        cout<<"copy constructor"<<endl;
    }
};
int main(){
    C c1;
    C c2 = C();
}

Output:

default constructor

default constructor

Question:

For C c1; default constructor will be called obviously, for C c2 = C(); I thought default constructor will be called to initialize a temporary object, then copy constructor will be call to initialize c2, It seems that I am wrong. why?

1 Answers
Related