问题描述
我最近读完了第一卷。
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.
我最近使用静态成员函数,并尝试使构造函数静态,为此编译器不开心。我检查了在书中的原因,但没有找到任何。
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:在看到一些回应后,因为我知道C#(和Java)允许构造函数声明为静态。
P.S.: 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.
推荐答案
构造函数的目的是初始化类的实例的内容。
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.
这篇关于为什么不能在c ++中将构造函数声明为static?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!