Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

My question is what does a constructor return? This question is not quite different from "What is the return type of a constructor?" I have read somewhere that a constructor returns a complete object implicitly (i.e implicit return type is the name of the class) but it shall not be specified explicitly.

struct empty{};

int main(){
   empty(); //creates a temporary and implicitly a constructor is called
}

So as per my interpretation the implicit return type should be the name of the class, in this case empty. Is my wild interpretation correct?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
219 views
Welcome To Ask or Share your Answers For Others

1 Answer

A constructor doesn't return anything. A constructor is called to initialize an object. A constructor can only be used to initialize an object; you can't actually call a constructor explicitly (for one thing, constructors do not have names).

In the example you give, empty() is not a function call expression, it is value initialization. It creates a value-initialized temporary object of type empty.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...