简体   繁体   中英

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. 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. 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?

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.

If you make a private data member const , the default assignment operator won't work and you will need to provide one.

But I heard that it is not common to make such variables private const without also doing private static const.

That's a useless generalisation.

Will the values differ between instances?

  • Yes : make them instance members;
  • No : make them static members.

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?

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. 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. 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.

I also need centerWidth, which will be set by dividing the width variable by 2. Can I do this if I >make them constants?

Yes, you can use a variable declared const in operations as normal:

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.

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. 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.

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.

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.

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

private:
  const int width;
};

int main()
{
  A(10);

  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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