简体   繁体   English

我应该将其声明为类的const int成员变量吗?

[英]Should I declare this as const int member variables of the class?

I have some values such as width, height among some other that I set in the constructor at this moment. 我现在在构造函数中设置了一些值,例如width,height。 They are not currently constants but I want them to be that so I will change them now. 它们目前不是常数,但我希望它们是常数,所以现在将对其进行更改。

But I heard that it is not common to make such variables private const without also doing private static const. 但是我听说,不同时进行私有静态const的情况下,将此类变量设为private const是不常见的。 Is this the case? 是这样吗 Or is it valid in this case? 还是在这种情况下有效? I also need centerWidth, which will be set by dividing the width variable by 2. Can I do this if I make them constants? 我还需要centerWidth,它将通过将width变量除以2来设置。如果我将它们设为常数,可以这样做吗?

Are these values specific to the instance of the object, but only set in the constructor? 这些值是特定于对象实例的,而是仅在构造函数中设置的吗? Then static does not make sense, as every object would have the same height and width. 那么static就没有意义,因为每个对象的高度和宽度都相同。

If you make a private data member const , the default assignment operator won't work and you will need to provide one. 如果将私有数据成员const为默认值,则默认赋值运算符将不起作用,并且需要提供一个。

But I heard that it is not common to make such variables private const without also doing private static const. 但是我听说,不同时进行私有静态const的情况下,将此类变量设为private const是不常见的。

That's a useless generalisation. 那是无用的概括。

Will the values differ between instances? 实例之间的值会有所不同吗?

  • Yes : make them instance members; :使其成为实例成员;
  • No : make them static members. :使其成为static成员。

That's all there is to it! 这里的所有都是它的!

I also need centerWidth, which will be set by dividing the width variable by 2. Can I do this if I make them constants? 我还需要centerWidth,它将通过将width变量除以2来设置。如果我将它们设为常数,可以这样做吗?

Yes, but consider doing this with an accessor function instead. 是的,但是请考虑使用访问器函数来执行此操作。

double T::centerWidth() {
   return this->width / 2;
}

I have some values such as width, height among some other that I set in the constructor at this >moment. 我在此时刻在构造函数中设置了一些值,例如width,height。 They are not currently constants but I want them to be that so I will change them now. 它们目前不是常数,但我希望它们是常数,所以现在将对其进行更改。

But I heard that it is not common to make such variables private const without also doing private >static const. 但是我听说,在不进行private> static const的情况下,将此类变量设为private const是不常见的。 Is this the case? 是这样吗

So, the way I would do this usually if you actually want them to be constant is as follows: 因此,如果您实际上希望它们保持不变,通常我会这样做:

// Header
class Widget
{
public:
    Widget();
    ~Widget();

    // rest of your functions/variables

private:
    static const int width;
    static const int height;

    // rest of your functions/variables
}

// Implementation
const int Widget::width = 640;
const int Widget::height = 800;

Widget::Widget()
{
    // do some construction work
}

// ... rest of your definitions

Or is it valid in this case? 还是在这种情况下有效?

It's valid if the members you declare static will be the same for each object instance of the class you create. 如果您声明为static的成员对于您创建的类的每个对象实例都是相同的,则这是有效的。

I also need centerWidth, which will be set by dividing the width variable by 2. Can I do this if I >make them constants? 我还需要centerWidth,它将通过将width变量除以2来设置。如果我使它们成为常数,可以这样做吗?

Yes, you can use a variable declared const in operations as normal: 是的,您可以像往常一样在操作中使用声明为const的变量:

const int a = 2;
int b = 2;
int c = a + b; // 4

Since you set your variables in the constructor, they are instance specific, so static does not make any sense. 由于您是在构造函数中设置变量的,因此它们是特定于实例的,因此static没有任何意义。

I know what problem you are trying to solve. 我知道您要解决什么问题。 You are trying to provide users read-only access to the width and height of an image, while allowing modifications from the class. 您试图为用户提供对图像宽度和高度的只读访问权限,同时允许从类中进行修改。 You can not do that by declaring member variables const. 您不能通过声明成员变量const来做到这一点。 All modification, including copy construction and assignment needs them to be non-const. 所有修改,包括副本构造和分配,都需要使其为非常量。

One solution is to use a public getter and a protected/private setter. 一种解决方案是使用公共获取器和受保护/私有设置器。 In my own class I use the public member functions called xs() and ys() to return xsize and ysize respectively. 在我自己的类中,我使用称为xs()ys()的公共成员函数分别返回xsize和ysize。

DO NOT EVEN THINK about declaring variables public const and using const_cast tricks to copy and assign, unless you like subtle, deep, pervasive bugs arising from improper compiler optimization and undefined behavior of const_cast. 甚至不必考虑将变量声明为public const并使用const_cast技巧进行复制和分配,除非您喜欢因编译器优化不当以及const_cast的未定义行为而引起的细微,深入,普遍的错误。

If you are not going to change these variables by member functions then you do should declare them const and initialize in constructor initialization list. 如果不打算通过成员函数更改这些变量,则应声明它们const并在构造函数初始化列表中进行初始化。

class A
{
public:
  A(int w) : width(w)
  {
  }

private:
  const int width;
};

int main()
{
  A(10);

  return 0;
}

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

相关问题 我怎样才能改进这个设计,迫使我声明一个成员函数const并声明变量是多变的? - How can I improve this design that forces me to declare a member function const and declare variables mutable? 我是否应该将将由C函数使用的成员变量声明为静态变量? - Should I declare member variables that are going to be used by C functions as static? 我应该声明这些方法是什么? - Should I declare these methods const? 即使成员函数修改了指针指向的内容,我也应该将其声明为const吗? - Should I declare a member function as `const` even if it modifies what a pointer points to? 是否可以同时声明一个类成员常量/非常量? - Is it possible to declare a class member both const/non-const? 如果它只是“浅常量”而不是“深常量”,我应该声明方法“常量”吗? - Should I declare method “const” if it is “shallow const” only, not “deep const”? 在typedef中使用const int类成员? - Using const int class member in typedef? 我应该在类或名称空间范围使用静态const变量吗? - Should I prefer static const variables at class or namespace scope? 我可以在C ++类中将成员变量声明为const吗? - Can i declare member variable as const in class of c++?if yes,how? 为什么静态const引用类成员变量? - Why not static const references on class member variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM