Home:ALL Converter>Is there any difference between C c; and C c = C();?

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

Ask Time:2016-03-08T15:35:06         Author:expoter

Json Formatter

#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?

Author:expoter,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/35861561/is-there-any-difference-between-c-c-and-c-c-c
yy