C++ 中的空构造函数:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14918025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:49:21  来源:igfitidea点击:

Empty Constructors in C++:

c++constructor

提问by FranXh

In my code I am doing the following, but I am not sure if I am "allowed" to or if it is a good designing technique. I need to create an empty constructor, but I also need a constructor that initializes the variables given the parameters. So I am doing the following:

在我的代码中,我正在执行以下操作,但我不确定我是否被“允许”或者它是否是一种好的设计技术。我需要创建一个空的构造函数,但我还需要一个构造函数来初始化给定参数的变量。所以我正在做以下事情:

This is the C.h file.

这是Ch文件。

 class C
 {
   private:
    string A;
    double B;
   public:
   //empty constructor
   C();
   C(string, double);  
 }

And my C.cpp file:

还有我的 C.cpp 文件:

//this is how I declare the empty constructor
 C::C()
  {

  }


  C::C(string a, double b)
  {
         A = a;
         B = b;
  }

Is the way I am declaring the empty constructor right or do I need to set A= NULL and B=0.0?

我声明空构造函数的方式是正确的还是需要设置 A= NULL 和 B=0.0?

回答by juanchopanza

Your empty constructor does not do what you want. The doubledata member will not be zero-initialized unless you do it yourself. The std::stringwill be initialized to an empty string. So the correct implementation of the default constructor would simply be

您的空构造函数不会执行您想要的操作。该double数据成员不会被初始化为零,除非你自己做。在std::string将被初始化为空字符串。所以默认构造函数的正确实现就是

C::C() : B() {} // zero-initializes B

Concerning the other constructor, you should prefer the initialization list:

关于其他构造函数,您应该更喜欢初始化列表:

C::C(const string& a, double b) : A(a), B(b) {}

otherwise, what you are doing is an assignmentto default constructed objects.

否则,您正在做的是对默认构造对象的赋值

回答by Mohammad Dehghan

It is fine to do this and leave the constructor empty, but you should be aware that uninitialized fields have undefined value. stringis a class and it's default constructor takes care of its initialization, but doubleis not initialized here (in your defualt constructor), and its value is undefined (it may be whatever value previously exists in the memory).

这样做并保留构造函数为空是没有问题的,但您应该知道未初始化的字段具有未定义的值。string是一个类,它的默认构造函数负责它的初始化,但double这里没有初始化(在你的默认构造函数中),它的值是未定义的(它可能是内存中先前存在的任何值)。

回答by Pete Becker

You are the only person who can answer this question, because it depends entirelyon your requirements for a default-constructed object. Since you haven't mentioned what your requirements are, it's not possible to give a definitive answer. Some people will guess that you should initialize Bto 0, but that decision should be based on your design, not on various notions of "good" programming practice.

您是唯一可以回答这个问题的人,因为这完全取决于您对默认构造对象的要求。由于您没有提到您的要求是什么,因此无法给出明确的答案。有些人会猜测您应该初始化B为 0,但该决定应该基于您的设计,而不是基于“良好”编程实践的各种概念。

回答by Mahmoud Badri

In C++11 and later you can use the following to generate a default no-param constructor:

在 C++11 及更高版本中,您可以使用以下内容生成默认的无参数构造函数:

C() = default;

This is neater than C(){}.

这比 C(){} 更简洁。

This doesn't initialize members. In C++11 you can initialize members in the same line of declaration:

这不会初始化成员。在 C++11 中,您可以在同一行声明中初始化成员:

int m_member = 0; // this is a class member

Those 2 features avoids having to create your own no param constructor to default initialize members. So your class can look like this when applying those 2 features:

这两个功能避免了必须创建自己的无参数构造函数来默认初始化成员。因此,在应用这 2 个功能时,您的类可能如下所示:

class C
{
private:
    string A;
    double B = 0;

public:
   C() = default;
   C(string, double);  
}

回答by billz

A is std::string, you can't set it to NULLbut can set to empty string and std::stringhas default constructor which initialize it to empty string by default.

A is std::string,您不能将其设置为,NULL但可以将其设置为空字符串,并且std::string具有默认构造函数,默认情况下将其初始化为空字符串。

C::C()
:B(0.0)
{
}

Maybe you need a constructor constructor with default parameter instead of two constuctors?

也许您需要一个带有默认参数的构造函数而不是两个构造函数?

C(const string& a= "", double b= 0.0)
: A(a),
  B(b)
{
}

回答by Daniel Gratzer

You can leave it as is. You can do this because both stringand doublecan be default constructed. Meaning that you can say string foo;and not get any errors.

你可以保持原样。你可以这样做,因为这两个stringdouble可默认构造。这意味着您可以说string foo;并且不会出错。

Contrast this with what happens here:

将此与此处发生的情况进行对比:

class Bar{
    private:
        Bar(); //Explicitly private;
};
Bar b;

Here we get an error about no constructor Bar::Bar()being found.

这里我们得到一个关于没有Bar::Bar()找到构造函数的错误。

As to whether it's a good idea: It's hard to say without knowing the situation this class will be used in. Perhaps it's perfectly sensible to have it be in a unconfigured position. But for many classes, such as a class representing a file for example, allowing a file object which doesn't point to any file is obviously wrong.

至于它是否是一个好主意:在不知道这个类将被使用的情况下很难说。也许让它处于未配置的位置是完全明智的。但是对于许多类,例如表示文件的类,允许不指向任何文件的文件对象显然是错误的。

回答by user258367

You are doing it correct and you can see it by compiling your code. Your default constructor will initialize the string and double by calling their default constructors. If you define any constructor in your class it will hide the default constructor created by compiler for you.

你做得对,你可以通过编译你的代码来看到它。您的默认构造函数将通过调用它们的默认构造函数来初始化 string 和 double。如果您在类中定义任何构造函数,它将隐藏编译器为您创建的默认构造函数。

You can improve your constructor code by writing it like this

您可以通过这样编写来改进构造函数代码

C::C(string a, double b):A(a), b(b)
{
}