Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

c++ 为什么用初始化列表的方式构造对象效率更高 | Nature 的博客 #16

Open
NatureLan-sudo opened this issue Aug 10, 2023 · 0 comments

Comments

@NatureLan-sudo
Copy link
Owner

https://naturelan-sudo.github.io/2022/10/13/%E4%B8%BA%E4%BB%80%E4%B9%88%E7%94%A8%E5%88%9D%E5%A7%8B%E5%8C%96%E5%88%97%E8%A1%A8%E6%95%88%E7%8E%87%E4%BC%9A%E6%9B%B4%E9%AB%98%20%20%20%20-%20%20%20%202022-10-09/

如果一个类二使用类一中的内容进行初始化,那么使用初始化列表的方式会更快。如果只是普通的数据类型没有区别,但是如果是自己定义的数据类型就会更快,因为初始化列表的方式会减少一次默认构造函数的调用。在进入构造函数体之前,使用初始化列表,不用调用默认构造函数,进入函数体后,在进行赋值初始化,需要调用默认构造函数,再进行一次赋值。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant