简体   繁体   English

为什么不能在c ++中将构造函数声明为static?

[英]Why can't constructor be declared as static in c++?

I have recently finished reading the 1st Vol. 我最近读完了第一卷。 of Thinking in C++ by Bruce Eckel and have now turned to applying the knowledge to some practical use. Bruce Eckel在C ++中的思考,现在转向将这些知识用于实际应用。

I was recently working with static member functions and tried making the constructor static, for which the compiler was unhappy. 我最近使用静态成员函数并尝试使构造函数静态,编译器对此不满意。 I checked for the reason in the book but couldn't find any. 我检查了书中的原因但找不到任何原因。

Can anyone explain why? 有谁能解释为什么?

PS: After seeing some responses, I would like to mention that the confusion arises from my knowledge that C# (and Java) allows constructors to be declared as static. PS:在看到一些回复之后,我想提一下,由于我知道C#(和Java)允许构造函数被声明为静态,所以引起了混淆。

The purpose of a constructor is to initialize the contents of an instance of the class. 构造函数的目的是初始化类实例的内容。

Static methods don't have an instance associated with them. 静态方法没有与之关联的实例。

Hence there is no such thing as a static constructor. 因此,没有静态构造函数。

The language as such does not provide such functionality but it can be simulated indirectly. 这样的语言不提供这样的功能,但可以间接模拟。 See this answer for more details. 有关详细信息,请参阅此答案 Not that I am really sure why you would ever need to do such a thing. 并不是说我真的很确定你为什么需要做这样的事情。

The constructor member function constructs the object as specified, using an existing allocation -- ie this is present. 构造函数成员函数使用现有分配构造指定的对象 - 即存在this对象。

static member functions specify no storage to an object, so there is no associated instance to construct -- ie this is absent. static成员函数指定没有存储到一个对象,所以没有关联的实例构造-即this是不存在的。 Therefore, you cannot specify a static constructor. 因此,您无法指定静态构造函数。

Perhaps you are looking for a named static member function which returns an instance by value: 也许您正在寻找一个命名的静态成员函数,该函数按值返回实例:

class t_object {
public:
  static t_object Func(…optional parameter list…) {
    t_object result(…ctor parameters…);
    …initialize result…
    return result;
  }
  ...
};

One very useful feature of C++ over C is it provides a proper way for proper initialization and clean-up when the instances of a user defined type are created so that you have a well formed object ready to start working with. C ++相对于C的一个非常有用的特性是,当创建用户定义类型的实例时,它提供了正确初始化和清理的正确方法,以便您有一个格式良好的对象可以开始使用。

The language achieves this through the mechanism of constructors and destructors. 语言通过构造函数和析构函数的机制实现了这一点。

As you might note that the reason why constructors and destructors exist are for maintaining the instances which are created. 您可能会注意到构造函数和析构函数存在的原因是为了维护创建的实例。

Now static implies or at-least is used when there is something common which all the objects can use. 现在static暗示或至少在所有对象都可以使用的共同点时使用。 You use it when there is really something which is to be shared among all the instances of the class which you create. 当您确实要在您创建的类的所有实例之间共享某些内容时,可以使用它。

And the interface to the static data members of the class is provided through static member functions which are mainly used on the static data members. 并且通过静态成员函数提供类的静态数据成员的接口,静态成员函数主要用于静态数据成员。

So, if the constructor is allowed to be made static what should it possibly mean so that the definition given to it by making it static is still on the lines of the reason why it came into picture(to properly initialize the object before you get hold of it). 因此,如果允许构造函数变为静态,那么它应该是什么意思,以便通过使其静态给定的定义still是它进入图片的原因(在您获得持有之前正确初始化对象)它)。 So, if there is no object then it doesn't make sense to have a constructor/destructor. 所以,如果没有对象,那么拥有一个构造函数/析构函数是没有意义的。

If you think on the above lines it doesn't make any sense to allow constructors to be static at least in this case(in C++). 如果您考虑上面的行,那么至少在这种情况下允许构造函数是静态的(在C ++中)是没有任何意义的。 Hence, it is not supported by this language. 因此,这种语言不支持它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM