tuzzer
tuzzer

Reputation: 1149

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

I tried to have one static const member variable to relate to another static const variable in a class. The motivation is that if I need to modify one value later (when coding), i don't need to change all of those that are related to each other one by one.

For example:

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

It won't compile and the error was ''Box::height' cannot appear in a constant-expression'. So I guess you must type in the value of a static const member. But is there a way to have one member relate to another member variable of the same class, given that they will all be static const??

Upvotes: 10

Views: 22707

Answers (5)

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;

Upvotes: 0

The exception to the initialization of a static data member inside the class declaration is if the static data member is a const of integral or enumeration 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;
}

Upvotes: -1

Basem Aljedai
Basem Aljedai

Reputation: 11

Try this out:

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 message error when I use double instead of int:

error: ‘box::height’ cannot appear in a constant-expression

I hope my post helps you ;)

Upvotes: 0

user206705
user206705

Reputation:

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

// box.h
#pragma once

class box
{
public:
static const float x;   
};

const float box::x = 1.0f;

Upvotes: 19

kennytm
kennytm

Reputation: 523724

In C++11 you could use constexpr:

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

Otherwise, you could use an inline function (but you need use call it as box::lid_height()), which a good optimizer should be able to reduce it to a constant on use:

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

Upvotes: 10

Related Questions