Member const 和Member 有什么区别?

zhuoyelee 2002-08-13 01:07:54
《c++ programming lanauage》是这样写的:

10.4.6.2Member Constants

It is also possible to initialize a static integral constant member by adding a constant-expression initializer to its member declaration. For example:

class :Curious {
static const int c1 =7; //ok ,but remember definition
static int c2 =11; // error : not const
const int c3 =13; // error: not static
static const int c4 = f(17); // error: in-class initializer not constant
static const float c5 =7.0; // error: in-class not integral
//....
};

If(and only if) you use an initalized member in a way that requires it to be stored as an object in memory, the member must be (uniquely) defined somewhere. The initializer may not be repeated:
const int Curious::c1; // neccessory, but don't repeat initializer here
const int* p= &Curious::c1; //ok Curious::c1 has been defined

那位高手能帮我解释一下上面的意思嘛?谢谢。
其中,static 和const 为什么一定要连用。
...全文
192 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
liushmh 2002-08-14
  • 打赏
  • 举报
回复
我认为
作为static 变量,只能初始化一次。
其实只用static 也可以,但是必须要在类体外初始化
如果放在类体内,
这样也就是将该变量的定义放在了类声明的头文件中了
如果一个程序中有多个文件要用到该头文件的话,
就会出错,因为此时static变量就被定义了多次
但是如果是常量就不会出现上述问题。
kof99th 2002-08-13
  • 打赏
  • 举报
回复
一句话,只有静态整型常量才能在类定义中初始化。
chinaeagle 2002-08-13
  • 打赏
  • 举报
回复
Static members in C++

There are times when you need a single storage space to be used by all objects of a class. In C, you would use a global variable, but this is not very safe. Global data can be modified by anyone, and its name can clash with other identical names in a large project. It would be ideal if the data could be stored as if it were global, but be hidden inside a class, and clearly associated with that class.


This is accomplished with static data members inside a class. There is a single piece of storage for a static data member, regardless of how many objects of that class you create. All objects share the same static storage space for that data member, so it is a way for them to “communicate” with each other. But the static data belongs to the class; its name is scoped inside the class and it can be public, private, or protected.


Defining storage for static data members

Because static data has a single piece of storage regardless of how many objects are created, that storage must be defined in a single place. The compiler will not allocate storage for you. The linker will report an error if a static data member is declared but not defined.


The definition must occur outside the class (no inlining is allowed), and only one definition is allowed. Thus, it is common to put it in the implementation file for the class. The syntax sometimes gives people trouble, but it is actually quite logical. For example, if you create a static data member inside a class like this:


class A {
static int i;
public:
//...
};
Then you must define storage for that static data member in the definition file like this:


int A::i = 1;
If you were to define an ordinary global variable, you would say


int i = 1;
but here, the scope resolution operator and the class name are used to specify A::i.


Some people have trouble with the idea that A::i is private, and yet here’s something that seems to be manipulating it right out in the open. Doesn’t this break the protection mechanism? It’s a completely safe practice for two reasons. First, the only place this initialization is legal is in the definition. Indeed, if the static data were an object with a constructor, you would call the constructor instead of using the = operator. Second, once the definition has been made, the end-user cannot make a second definition – the linker will report an error. And the class creator is forced to create the definition or the code won’t link during testing. This ensures that the definition happens only once and that it’s in the hands of the class creator.


The entire initialization expression for a static member is in the scope of the class. For example,


//: C10:Statinit.cpp
// Scope of static initializer
#include <iostream>
using namespace std;

int x = 100;

class WithStatic {
static int x;
static int y;
public:
void print() const {
cout << "WithStatic::x = " << x << endl;
cout << "WithStatic::y = " << y << endl;
}
};

int WithStatic::x = 1;
int WithStatic::y = x + 1;
// WithStatic::x NOT ::x

int main() {
WithStatic ws;
ws.print();
} ///:~
Here, the qualification WithStatic:: extends the scope of WithStatic to the entire definition.


static array initialization
Chapter 8 introduced the static const variable that allows you to define a constant value inside a class body. It’s also possible to create arrays of static objects, both const and non-const. The syntax is reasonably consistent:


//: C10:StaticArray.cpp
// Initializing static arrays in classes
class Values {
// static consts are initialized in-place:
static const int scSize = 100;
static const long scLong = 100;
// Automatic counting works with static arrays.
// Arrays, Non-integral and non-const statics
// must be initialized externally:
static const int scInts[];
static const long scLongs[];
static const float scTable[];
static const char scLetters[];
static int size;
static const float scFloat;
static float table[];
static char letters[];
};

int Values::size = 100;
const float Values::scFloat = 1.1;

const int Values::scInts[] = {
99, 47, 33, 11, 7
};

const long Values::scLongs[] = {
99, 47, 33, 11, 7
};

const float Values::scTable[] = {
1.1, 2.2, 3.3, 4.4
};

const char Values::scLetters[] = {
'a', 'b', 'c', 'd', 'e',
'f', 'g', 'h', 'i', 'j'
};

float Values::table[4] = {
1.1, 2.2, 3.3, 4.4
};

char Values::letters[10] = {
'a', 'b', 'c', 'd', 'e',
'f', 'g', 'h', 'i', 'j'
};

int main() { Values v; } ///:~
With static consts of integral types you can provide the definitions inside the class, but for everything else (including arrays of integral types, even if they are static const) you must provide a single external definition for the member. These definitions have internal linkage, so they can be placed in header files. The syntax for initializing static arrays is the same as for any aggregate, including automatic counting.


You can also create static const objects of class types and arrays of such objects. However, you cannot initialize them using the “inline syntax” allowed for static consts of integral built-in types:


//: C10:StaticObjectArrays.cpp
// Static arrays of class objects
class X {
int i;
public:
X(int ii) : i(ii) {}
};

class Stat {
// This doesn't work, although
// you might want it to:
//! static const X x(100);
// Both const and non-const static class
// objects must be initialized externally:
static X x2;
static X xTable2[];
static const X x3;
static const X xTable3[];
};

X Stat::x2(100);

X Stat::xTable2[] = {
X(1), X(2), X(3), X(4)
};

const X Stat::x3(100);

const X Stat::xTable3[] = {
X(1), X(2), X(3), X(4)
};

int main() { Stat v; } ///:~
The initialization of both const and non-const static arrays of class objects must be performed the same way, following the typical static definition syntax.


kwok_1980 2002-08-13
  • 打赏
  • 举报
回复
象上面的定义,如果在VC下是不能通过的,但,在gcc上面是可以的.
所以它是符合C++ Standard的.
static const int c1 =7;-->上面的意思说定义了一个对象且初始化.
且对象在内存中是唯一定义的,不能重复定义.但,这样在class定义并初始化
的static const的对象,就像一般的static一样要在globle上声明一下,但
不需要再初始化啦!但,这样初始化后的对象,同时可以作为赋值对象.
照上面的意思就是说在class内要static和const连用才可以定义且同时
初始化一个对象!
zheng_can 2002-08-13
  • 打赏
  • 举报
回复
gz

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧