博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Name control
阅读量:4346 次
发布时间:2019-06-07

本文共 4951 字,大约阅读时间需要 16 分钟。

static:        (Page 406)

In both C and C++ the keyword static has two basic meanings, which unfortunately often step on each other's toes.

1 Allocated once at a fixed address; that is, the object is created in a special static data area rather than on the stack each time a function is called. This is the concept of static storage.

2 Local to a particular translation unit( and local to a class scope in C++). Here, static controls the visibility of a name, so that name cannot be seen outside the translation unit or class. This also describes the concept of linkage, which determines what names the linker will see.

 

 

static variables inside functions         (Page 407)

C and C++ allow you to create a static object inside a function; the srotage for this object is not on the stack but instead in the program's static data area. This object is initialized only once, the first time the function is called, and then retains its value between function invocations.

 

static class objects inside functions      (Page 408)

The rules are the same for static objects of user-defined types, including the fact that some initialization is requred for the object. However, assignment to zero has meaning only for built-in types; user-defined types must be initialized with constructor calls. Thus, if you donot specify constructor arguments when you define the static object, the class must have a default constructor. This construction occurs the first time control passes through the definition, and only the first time.

 

static class object destructors        (Page 409)

Destructor for static objects(that is, all objects with static storage, not just local static objects) are called when main() exits or when the Standard C libraty function exit() is explicitly called. This means that it can be dangerous to call exit() inside a destructor because you can end up with infinite recursion. Static object destructors are not called if you exit the program using the Standard C library function abort().

In C++, the constructor for a global static object is called before main() is entered, so you now have a simple and portable way to execute code before entering main() and to execute code with the destructor after exiting().

 

Controlling linkage          (Page 412)

An object or function name at file scope that is explicitly declared static is local to its translation unit. That name has internal linkage. This means that you can use the same name in other translation units without a name clash.

 

static members in C++        (Page 424)

 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 a single piece of srotage for a static data member, regardless of how many objects of that class you create.

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.

For example,

class A{

  static int i;

};

int A::i = 1;

 

static array initialization        (Page 425)

it allows static const variable defines inside a class body. With static const 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.

For example,

class value{

  static const int size = 100;

  static const int sc[];

  static int cc[];

};

const int value::sc[] = {1,2};

int value::cc[] = {2,3};

 

static member functions        (Page 429)

You can also create static member functions that, like static data members, work for the class as a whole rather than for a particular object of a class.

You can call a static member function in the ordinary way, with the dot or the arrow, in association with an object. However, it is more typical to call a static member function by itself, without any specific object, using the scope-resolution operator.

For example,

class X{

  static void f(){}

};

int main()

{

  X::f();

}

A static member function cannot access ordinary data members, only static data members. It can call only other static member functions. Normally, the address of the current object(this) is quietly passed in when any member function is called, but a static member has no this pointer, which is the reason it cannot access ordinary members.

 

static initialization dependency        (Page 432)

Within a specific translation unit, the order of initialization of static objects is guaranteed to be the order in which the object definitions appear in that translation unit. The order of destruction is guaranteed to be the reverse of the order of initialization. However, there is no guarantee concerning the order of initialization of static objects across translation units.

转载于:https://www.cnblogs.com/ruccsbingo/p/3753958.html

你可能感兴趣的文章
JAVA 基础 / 第八课:面向对象 / JAVA类的方法与实例方法
查看>>
Ecust OJ
查看>>
P3384 【模板】树链剖分
查看>>
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
Equivalent Strings
查看>>
收藏其他博客园主写的代码,学习加自用。先表示感谢!!!
查看>>
H5 表单标签
查看>>
C语言编程-9_4 字符统计
查看>>
在webconfig中写好连接后,在程序中如何调用?
查看>>
限制用户不能删除SharePoint列表中的条目(项目)
查看>>
feign调用spring clound eureka 注册中心服务
查看>>
ZT:Linux上安装JDK,最准确
查看>>
LimeJS指南3
查看>>
关于C++ const成员的一些细节
查看>>
《代码大全》学习摘要(五)软件构建中的设计(下)
查看>>
C#检测驱动是否安装的问题
查看>>
web-4. 装饰页面的图像
查看>>
微信测试账户
查看>>