How to declare a static constant member variable of a class that includes some simple calculations?

I tried to bind one static constant member variable to another static constant variable in the class. The motivation is that if I need to change one value later (when encoding), I do not need to change all those that are connected to each other one at a time.

For instance:

class Box { public: Box(); private: static const double height = 10.0; static const double lid_height = 0.5 + height; }; 

It will not compile, and the โ€œBox :: heightโ€ error cannot appear in the constant expression. "Therefore, I assume that you should enter the value of a static constant member. But is there a way to associate one member with another member variable of the same class, given that they will all be static?

+6
source share
5 answers

Set the value of your static const member variables outside the class declaration using the following syntax.

 // box.h #pragma once class box { public: static const float x; }; const float box::x = 1.0f; 
+16
source

In C ++ 11, you can use constexpr :

 class box { public: box(); private: static constexpr double height = 10.0; static constexpr double lid_height = 0.5 + height; }; 

Otherwise, you can use the built-in function (but you need to use it as box::lid_height() ), that a good optimizer should be able to reduce it to constant when using:

 class box { public: box(); private: static const double height = 10.0; static double lid_height() { return 0.5 + height; } }; 
+10
source

Try the following:

 private: static const int height = 100; static const int lid_height = 05 + height; 

This will work, I think the problem is with float (double) numbers. My compiler gave the following error message when I use double instead of int:

error: 'box :: height cannot be displayed in constant expression

I hope my post helps you;)

0
source

One more example

 foo.h class foo { static const string s; // Can never be initialized here. static const char* cs; // Same with C strings. static const int i = 3; // Integral types can be initialized here (*)... static const int j; // ... OR in cpp. }; foo.cpp #include "foo.h" const string foo::s = "foo string"; const char* foo::cs = "foo C string"; const int foo::j = 4; 
0
source

An exception to initializing a static data element inside a class declaration is that the static data member is a constant of an integral or enumerated type

 #include <iostream> class Car { enum Color {silver = 0, maroon, red }; int year; int mileage = 34289; // error: not-static data members // only static const integral data members // can be initialized within a class static int vin = 12345678; // error: non-constant data member // only static const integral data members // can be initialized within a class static const string model = "Sonata"; // error: not-integral type // cannot have in-class initializer static const int engine = 6; // allowed: static const integral type }; int Car::year = 2013; // error: non-static data members // cannot be defined out-of-class int main() { return 0; } 
-1
source

Source: https://habr.com/ru/post/908944/


All Articles