C++ 如何声明涉及一些简单计算的类的静态常量成员变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9371295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to declare a static constant member variable of a class that involves some simple calculations?
提问by tuzzer
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??
它不会编译,错误是“Box::height”不能出现在常量表达式中。所以我猜你必须输入一个静态常量成员的值。但是有没有办法让一个成员与同一类的另一个成员变量相关,因为它们都是静态常量?
回答by
Set the value of your static const member variables outside of the class declaration, using the following syntax.
使用以下语法在类声明之外设置静态 const 成员变量的值。
// box.h
#pragma once
class box
{
public:
static const float x;
};
const float box::x = 1.0f;
回答by kennytm
In C++11 you could use constexpr
:
在 C++11 中,您可以使用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:
否则,您可以使用内联函数(但您需要使用将其称为 as box::lid_height()
),一个好的优化器应该能够在使用时将其减少为常量:
class box
{
public:
box();
private:
static const double height = 10.0;
static double lid_height() { return 0.5 + height; }
};
回答by Basem Aljedai
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:
这会起作用,我认为问题出在浮点数(双精度数)上。当我使用 double 而不是 int 时,我的编译器给出了以下消息错误:
error: ‘box::height' cannot appear in a constant-expression
错误:“box::height”不能出现在常量表达式中
I hope my post helps you ;)
希望我的帖子能帮到你;)
回答by Ashish Surykant Ghadge
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;
回答by Ashish Surykant Ghadge
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;
}