chap10 name control

来源:互联网 发布:莎莎源码 编辑:程序博客网 时间:2024/05/10 13:01
 
namespace特点:
namespace myLib
{
....
}//(1)没有“;”
(2)可以在多个头文件里定义,不算redefinition
(3)A namespace definition can appear only at global scope, or nested within
another namespace.
(4)赋值,如果原来的namespace的名字太长,可以用简单的名字代替
e.g namespace shortName = tooLongName;


static member data & static member function
//: C10:StaticMemberFunctions.cpp
class X {
 int i;
 static int j;
public:
 X(int ii = 0) : i(ii) {
    // Non-static member function can access
    // static member function or data:
   j = i;
 }
 int val() const { return i; }
 static int incr() {
   //! i++; // Error: static member function
   // cannot access non-static member data
   return ++j;
 }
 static int f() {
   //! val(); // Error: static member function
   // cannot access non-static member function
   return incr(); // OK -- calls static
 }
};

int X::j = 0;//初始化静态成员变量

int main() {
 X x;
 X* xp = &x;
 x.f();//also works
 xp->f();//also works
 X::f(); // Only works with static members
} ///:~

NOTE: 当对象调用一般的成员函数时,都会传入一个this指针,而对于静态成员函数,
是属于整个类的,不会传入this指针,所以不能调用一般的成员变量和一般的成员函数。


alternate linkage specificationse.g
extern "C" {
 float f(int a, char b);
 double d(int a, char b);
 #include "xxxx.h"
}
原创粉丝点击