简体   繁体   English

类的静态成员是否可以与C ++中的成员属于同一类型

[英]Can a static member of a class as the same type as the class it is member of in C++

lets say I have 可以说我有

class : foo
{
  public:
  static const foo Invalidfoo;
  foo();
  foo(int, string);
  private:
  int number;
  std::string name;
};

Is it safe or prone to any problem? 它安全还是容易出问题?

EDIT : 编辑:

I want to use this to have an invalid object to return as a reference to launch errors. 我想用它来返回一个无效的对象作为启动错误的参考。

It's legal. 是合法的

It's actually widely used in the singleton pattern 它实际上广泛用于单例模式

Singletons multi threading access and creation problems. 单例多线程访问和创建问题。

A nice article about this: 一篇不错的文章:

C++ and the Perils of Double-Checked Locking C ++和双重检查锁定的风险

It is perfectly legal, but the following is better: 这是完全合法的,但是以下更好:

class foo:
{
public:
    static const& foo Invalidfoo()
    {
        static foo Invalidfoo_;
        return Invalidfoo_;
    }

private:
      foo();
};

This way you are guaranteed that the object is initialized the first time it is used. 这样,可以确保在第一次使用对象时初始化该对象。

Edit: But no matter how you do it, you still have a global object, and that can be a cause of problem. 编辑:但是,无论如何操作,您仍然有一个全局对象,这可能是问题的原因。 The best solution may be to call the default constructor each time you need a default constructed object. 最好的解决方案可能是每次需要默认构造对象时都调用默认构造函数。 In terms of efficiency, the difference is probably negligable. 就效率而言,差异可能微不足道。

It is just acting like a global variable or singleton. 它只是像全局变量或单例。 It's prone to the problems relating to those. 容易出现与此有关的问题。

That is perfectly valid code. 那是完全有效的代码。 It doesn't have any reason to cause any problem, because static data members don't contribute to the size of the class. 它没有任何理由引起任何问题,因为静态数据成员不会增加类的大小。 No matter how many static data members you define in a class, it doesn't change its size by even one byte! 无论您在一个类中定义了多少个静态数据成员,它的大小都不会改变一个字节!

struct A
{
   int i;
   char c;
};

struct B
{
   int i;
   char c;
   static A a;
   static B b;
};

In above code, sizeof(A) == sizeof(B) will always be true. 在上面的代码中, sizeof(A) == sizeof(B)将始终为true。 See this demo: 观看此演示:

Its supported by the section $9.4.2/1 from the C++ Standard (2003), 由C ++ Standard(2003)的$ 9.4.2 / 1部分支持,

A static data member is not part of the subobjects of a class. 静态数据成员不属于类的子对象。 There is only one copy of a static data member shared by all the objects of the class. 该类的所有对象共享一个静态数据成员的副本

You cannot define non-static data member of the enclosing class type, because non-static members are parts of the object, and so they do contribute to the size of the class. 您不能定义封闭类类型的非静态数据成员,因为非静态成员是对象的一部分,因此它们确实会增加类的大小。 It causes problem when calculating the size of the class, due to recursive nature of the data members. 由于数据成员的递归性质,在计算类的大小时会引起问题。


See this topic: 请参阅此主题:

It's legal. 是合法的 Terrible code from a practical/style point of view, but it is legal, and technically, it can be made to work. 从实用/样式的角度来看,这是糟糕的代码,但是它是合法的,并且从技术上讲,它可以正常工作。 Better than Singleton because it's immutable. 比Singleton更好,因为它是不变的。

This is actually how a singleton is implemented, except your static member would be a pointer. 实际上,这是实现单例的方式,除了您的静态成员是指针。 So yes, you're safe. 是的,您很安全。

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

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