友元 static 类成员

友元机制允许一个类将对其非公有成员的访问权授予指定的函数或类

 

友元的声明以关键字 friend 开始,它只能出现在类定义的内部

 

友元声明可以出现在类中的任何地方:友元不是授予友元关系的那个类的成员,所以它们不受声明出现部分的访问控制影响


通常,将友元声明成组地放在类定义的开始或结尾是个好主意

 

友元可以是普通的非成员函数,或前面定义的其他类的成员函数,或整个类

 

将一个类设为友元,友元类的所有成员函数都可以访问授予友元关系的那个类的非公有成员

 

一般地讲,必须先定义包含成员函数的类,才能将成员函数设为友元;另一方面,不必预先声明类和非成员函数来将它们设为友元

 

友元声明将已命名的类或非成员函数引入到外围作用域中,此外,友元函数可以在类的内部定义,该函数的作用域扩展到包围该类定义的作用域


用友元引入的类名和函数(定义或声明),可以像预先声明的一样使用

 

类必须将重载函数集中每一个希望设为友元的函数都声明为友元


static 类成员

 

static 数据成员独立于该类的任意对象而存在;每个 static 数据成员是与类关联的对象,并不与该类的对象相关联

 

static 成员函数没有 this 形参,它可以直接访问所属类的 static 成员,但不能直接使用非 static 成员


使用类的 static 成员的优点
1. static 成员的名字是在类的作用域中,因此可以避免与其他类的成员或全局对象名字冲突
2. 可以实施封装。static 成员可以是私有成员,而全局对象不可以
3. 通过阅读程序容易看出 static 成员是与特定类关联的,可清晰地显示程序员的意图

在成员声明前加上关键字 static 将成员设为 static,static 成员遵循正常的公有/私有访问规则

 

可以通过作用域操作符从类直接调用 static 成员,或者通过对象、引用或指向该类类型对象的指针间接调用类的 static 成员

 

类成员函数可以不用作用域操作符来引用类的 static 成员

 

在类的外部定义 static 成员时,无须重复指定 static 保留字,该保留字只出现在类定义体内部的声明处

 

static 成员函数不能被声明为 const

 

static 成员函数也不能被声明为虚函数

 

static 数据成员必须在类定义体的外部定义(正好一次)

 

不像普通数据成员,static 成员不是通过类构造函数进行初始化,而是应该在定义时进行初始化

 

保证对象正好定义一次的最好办法,就是将 static 数据成员的定义放在包含类非内联成员函数定义的文件中

 

像使用任意的类成员一样,在类定义体外部引用类的 static 成员时,必须指定成员是在哪个类中定义的,然而,static 关键字只能用于类定义体内部的声明中,定义不能标示为 static

 

只要初始化式是一个常量表达式,整型 const static 数据成员就可以在类的定义体中进行初始化

 

const static 数据成员在类的定义体中初始化时,该数据成员仍必须在类的定义体之外进行定义,在类内部提供初始化式时,成员的定义不必再指定初始值


static 成员不是类对象的组成部分,独立于任何对象而存在

 

static 数据成员的类型可以是该成员所属的类类型,非 static 成员被限定声明为其自身类对象的指针或引用

 

static 数据成员可用作默认实参


非 static 数据成员不能用作默认实参,因为它的值不能独立于所属的对象而使用
ISO C++ forbids in-class initialization of non-const static member 'somemember'

 

#include <iostream>
#include <string>
using namespace std;

class X {
    friend class Y;
    friend void f() { /* ok to define friend function in the class body */ }
};
class Z {
    Y *ymem; // ok: declaration for class Y introduced by friend in X
    void g() { return ::f(); } // ok: declaration of f introduced by X
};

class Y {
      //todo...      
};

class Account {
public:
    // interface functions here
    void applyint() { amount += amount * interestRate; }
    static double rate() { return interestRate; }
    static void rate(double); //set interestRate
private:
    string owner;
    double amount;
    static const int period = 30; //ok!! period is const static data member of integral type 
    static double interestRate;
    static double initRate();
};
double Account::initRate()
{
     return 0.05;     
}
void Account::rate(double newRate)
{ 
     interestRate = newRate;
}
// define and initialize static class member
//interestRate is in the scope of the class 
//and hence has access to the private member initRate() of the class 
double Account::interestRate = initRate();

// definition of static member with no initializer;
// the initial value is specified inside the class definition
const int Account::period;

class Bar {
public:
 // ...
private:
     static Bar mem1; // ok
     Bar *mem2;       // ok
     // Bar mem3;       error
};

class Screen {
public:
    // bkground refers to the static member
    // declared later in the class definition
    Screen& clear(char = bkground);
private:
    static const char bkground = '#';
};

     
int main()
{  
    
    Account ac1;
    Account *ac2 = &ac1;
    // equivalent ways to call the static member rate function
    double rate;
    rate = ac1.rate();        // through an Account object or reference
    cout << "ac1.rate() --> " << rate << endl;
    rate = ac2->rate();       // through a pointer to an Account object
    cout << "ac2->rate() --> " << rate << endl;
    rate = Account::rate();   // directly from the class using the scope operator
    cout << "Account::rate() --> " << rate << endl;
     
    return 0;
}

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值