默认构造函数是什么概念?

发布于 2024-12-07 22:25:10 字数 330 浏览 0 评论 0原文

通过示例帮助我了解默认构造函数的概念。 我不知道何时在程序中使用默认构造函数,何时不使用。 帮我解决这个问题。用一个例子来解释一下。 什么时候需要使用它?

#include<iostream>
using namespace std;

class abc
{
public:
    abc()
    {
        cout<<"hello";
    }
};

int main()
{
    abc a;
    system("pause");
    return 0;
}

那么实际上默认构造函数有什么用以及什么时候需要使用它呢?

help me in getting the concept of default constructor with example.
i don't know when to use default constructor in the program and when not to.
help me coming over this problem.explain it with an example for me.
when it is necessary to use it?

#include<iostream>
using namespace std;

class abc
{
public:
    abc()
    {
        cout<<"hello";
    }
};

int main()
{
    abc a;
    system("pause");
    return 0;
}

so actually what is the use of default constructor and when it is necessary to use it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉薄对峙 2024-12-14 22:25:10

符合 DefaultConstrutible 概念的类允许使用以下表达式(N3242 第 17.6.3.1 段):

T u; // object is default initialized
T u{}: // object is value intialized
T(); T{}; // value initialized temporary

这个概念就这么多了。第 12.1/5 段实际上告诉我们什么是默认构造函数

类 X 的默认构造函数是类 X 的构造函数:
可以在没有参数的情况下调用。如果没有用户声明
类 X 的构造函数,没有参数的构造函数是
隐式声明为默认(8.4)。隐式声明的默认值
构造函数是其类的内联公共成员。 ...

随着删除的特殊成员函数的引入,该标准还定义了一系列没有隐式默认构造函数可用的情况以及平凡和非平凡默认构造函数的区别。

A class that conforms to the concept DefaultConstrutible allows the following expressions (paragraph 17.6.3.1 of N3242):

T u; // object is default initialized
T u{}: // object is value intialized
T(); T{}; // value initialized temporary

So much for the concept. Paragraph 12.1/5 actually tells us what a default constructor is

A default constructor for a class X is a constructor of class X that
can be called without an argument. If there is no user-declared
constructor for class X, a constructor having no parameters is
implicitly declared as defaulted (8.4). An implicitly-declared default
constructor is an inline public member of its class. ...

With the introduction of deleted special member functions, the standard also defines a list of cases where no implicit default constructor is available and the distinction of trivial and non-trivial default constructors.

后知后觉 2024-12-14 22:25:10

如果您的类实例化时不需要执行任何操作。使用默认构造函数,否则在任何情况下您都必须使用自己的构造函数,因为默认构造函数基本上不执行任何操作。
您也不需要编写任何“默认”构造函数。

class abc {
};

int main() {
abc a;  //don't want to do anything on instatiation
system("pause");
return 0;
}

class abc {
private:
int a;
public:
abc(int x) { a = x };
}


int main() {
abc a(1); //setting x to 1 on instantiation
system("pause");
return 0;
}

If you don't need to do anything as your class is instantiated. Use the default constructor, any situation else you will have to use your own constructor as the default constructor basically does nothing.
You also don't need to write any "default" constructor.

class abc {
};

int main() {
abc a;  //don't want to do anything on instatiation
system("pause");
return 0;
}

class abc {
private:
int a;
public:
abc(int x) { a = x };
}


int main() {
abc a(1); //setting x to 1 on instantiation
system("pause");
return 0;
}
樱花坊 2024-12-14 22:25:10
  • 构造函数是一个特殊的函数,没有返回类型。它的名称必须与类\结构名称相同。正如 Kerrek-SB 指出的那样,它没有实际的函数名称。
  • 默认构造函数是没有参数的构造函数,或者参数全部带有默认值的构造函数。
  • 构造函数仅被调用一次 - 当实例化对象时
  • 通过new表达式或初始化表达式调用构造函数。它不能被称为“手动”。
  • 对于初始化对象的字段很有用,通常使用成员初始值设定项列表

检查这个。

  • Constructor is a special function, without return type. Its name must be as the class\struct name. It doesn't have an actual name as a function, as Kerrek-SB pointed out.
  • Default constructor is the one that has no parameters, or has parameters all with a default value.
  • Constructor function is being called only once - when an object is instantiated
  • Constructor is called through a new expression or an initialization expression. It cannot be called "manually".
  • Useful for initializing object's fields, usually with a member initializer list.

Check this.

够运 2024-12-14 22:25:10

默认构造函数是不带参数的构造函数,将在以下情况下调用:

  • 在不带任何构造函数的情况下实例化或新建类的对象,例如:

    <前><代码>abc a;
    abc* aptr=新 abc;

  • 声明类的数组,例如:

    abc a_array[10];
    
  • 当您有一个不调用基类构造函数之一的继承类时

  • 当您的类中具有来自另一个类的功能并且您不调用该功能的类的明确构造函数时。
  • 当你使用一些标准库的容器如vector时,例如:

    向量 abc_列表;
    

在这些情况下你必须有一个默认的构造函数,否则如果你没有任何构造函数,编译器将隐式默认没有任何操作的构造函数,如果你有一些构造函数,编译器会显示编译错误。
如果您想要执行上述操作之一,请使用默认构造函数来确保每个对象都被正确实例化。

Default constructor is constructor with no argument and will be called on these situations:

  • Instancing or newing an object of a class without any constructor, like:

    abc a;
    abc* aptr=new abc;
    
  • Declaring an array of a class, like:

    abc a_array[10];
    
  • When you have a inherited class which does not call one of base class constructors

  • When you have a feature in your class from another class and you don't call a definite constructor of that feature's class.
  • When you use some containers of standard library such as vector, for example:

    vector <abc> abc_list;
    

In these situations you have to have a default constructor, otherwise if you do not have any constructor, the compiler will make an implicit default constructor with no operation, and if you have some constructors the compiler will show you a compile error.
If you want to do one of the above things, use a default constructor to make sure every object is being instantiated correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文