May 15, 2012

MaQzma MaQzma
Lab Rat
36 posts

Place to declare constants.

 

Hello,

My name is Mario, I’m new to this forum
and I sincerely apologize for my bad English.

I am having a dilemma about which is the best place to declare constants.
For example, I have an item that is derived from QGraphicsItem,
this element has two constant colors: pen color and brush color.

I’m thinking of declaring them as private constants:

  1. class Rect : public QGraphicsItem
  2. {
  3.  ....
  4.     private:
  5.         static const int _penColor = 0;
  6.         static const int _brushColor = 1;
  7. };

But it also can be declared as definitions in the header:

  1. #define PENCOLOR 0
  2. #define BRUSHCOLOR 1

As well as local variables:

  1. void Rect::draw()
  2. {
  3.     const int penColor = 0;
  4.     const int brushColor = 1;
  5. }

Maybe this is not important, but I wish I know what you think?

Thanks, Mariø.

 Signature 

Mario.

5 replies

May 15, 2012

Wilk Wilk
Lab Rat
119 posts

Hello.
The best way is using of private non static constant members. Also, if you want be able to change these parameters during app running then you’d better declare them non constant.
The members should be non static because it’ll be better for derived classes.
Also you may declare these members as properties.

May 15, 2012

Serg Serg
Lab Rat
16 posts

Hello.
Avoid using of #define declarations, because it’s preprocessors constant and makes your program hard to debug.
For compile-time constant in class you can use unnamed enum.

  1.     class Rect : public QGraphicsItem
  2.     {
  3.      ....
  4.         private:
  5.             enum {
  6.                 kPenColor = 0,
  7.                 kBrushColor = 1
  8.             };
  9.     };

May 16, 2012

MaQzma MaQzma
Lab Rat
36 posts

Thinking OOP: Is a good idea to declare these members as properties even when I don’t want to change these values?….Not sarcasm is a real question xP

 Signature 

Mario.

May 16, 2012

Wilk Wilk
Lab Rat
119 posts

Hello.
It’s a good idea if changing of these values doesn’t argue with those semantics. I mean, if these are real colors used for drawing, then it’s all right to make them properties, but if these are just a kind of constants for compile time (like preprocessor definitions), then it’s wrong to make them properties.
And of cause if you don’t ever want to change these values, then don’t declare them as properties.

May 18, 2012

MaQzma MaQzma
Lab Rat
36 posts

Wilk wrote:
If these are real colors used for drawing, then it’s all right to make them properties.

Yes, these are real colors. I mean objects.

Wilk wrote:
And of cause if you don’t ever want to change these values, then don’t declare them as properties. Yes, I don’t want to change these values.

Probably I will use local variables as constants:

  1. void Rect::draw()
  2. {
  3.     const QColor penColor();
  4.     const QColor brushColor();
  5. }

@Serg Thanks for this tip:

wrote:
For compile-time constant in class you can use unnamed enum.
 Signature 

Mario.

 
  ‹‹ Using Qt in Eclipse... without qmake?      How do I compile my Qt Application for different systems? ››

You must log in to post a reply. Not a member yet? Register here!