chap10 name control

 
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"
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值