C++ Constructors have no return type. Just exactly why?

前端 未结 8 827
孤独总比滥情好
孤独总比滥情好 2021-01-04 14:00

I\'ve Googled this and read many posts, but there are so many different answers that all make logical sense that I was wondering if an expert on the topic could demystify th

8条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-04 14:22

    The constructor does not specify a return type because it would be redundant: there is no type other than the one being constructed that a constructor could potentially "return". I put "return" in quotes because technically constructors do not return anything: when they are invoked in a static context, they initialize an instance in place; when they are invoked in a dynamic context, it is the operator new that returns something, not a constructor.

提交回复
热议问题