gpt4 book ai didi

c++ - 我们可以说构造函数创建对象吗?

转载 作者:太空狗 更新时间:2023-10-29 20:03:18 24 4
gpt4 key购买 nike

有人告诉我构造函数创建对象。但是在互联网上我搜索了对象创建时执行的构造函数。你能解释一下吗?我是 C++ 新手

最佳答案

In C++ a constructor is a special kind of class member function that is executed when an object of that class is instantiated.

Constructors are typically used to initialize member variables of the class to appropriate default values, or to allow the user to easily initialize those member variables to whatever values are desired.

所以当你调用构造函数时你有一个已经实例化的对象,所以构造函数不会创建对象,也不会创建对象变量,它只是用来初始化该对象内部的变量(或者做一些任务你想要在对象被使用之前)。

编辑:还有:

A constructor performs its work in this order:

  1. It calls base class and member constructors in the order of declaration.
  2. If the class is derived from virtual base classes, it initializes the object's virtual base pointers.
  3. If the class has or inherits virtual functions, it initializes the object's virtual function pointers. Virtual function pointers point to the class's virtual function table to enable correct binding of virtual function calls to code.
  4. It executes any code in its function body.

查看这些链接以获取更多信息:

http://www.learncpp.com/cpp-tutorial/85-constructors/

https://msdn.microsoft.com/en-us/library/s16xw1a8.aspx

https://isocpp.org/wiki/faq/ctors

关于c++ - 我们可以说构造函数创建对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30138449/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com