const&内联&静态成员&友元

    1. const成员函数
    2. 内联
    3. 友元
    4. 静态成员

const修饰成员函数

在成员函数后面加const,const修饰this指针所指向的对象,也就是保证调用这个const成员函数的对象
在函数内不会被改变。
class Date
{
public :
void Display ()
{
cout<<"Display ()" <<endl;
cout<<"year:" <<_year<< endl;
cout<<"month:" <<_month<< endl;
cout<<"day:" <<_day<< endl<<endl ;
}
void Display () const
{
cout<<"Display () const" <<endl;
cout<<"year:" <<_year<< endl;
cout<<"month:" <<_month<< endl;
cout<<"day:" <<_day<< endl<<endl;
}
private :
int _year ; // 年
int _month ; // 月
int _day ; // 日
};
void Test ()
{
Date d1 ;
d1.Display ();
const Date d2;
d2.Display ();
}

注意:
1. const对象可以调用非const成员函数和const成员函数吗?
- 不可以 - 可以
2. 非const对象可以调用非const成员函数和const成员函数?
- 可以 - 可以
3. const成员函数内可以调用其他的const成员函数和非const成员函数吗?
- 可以 - 不可以
4. 非const成员函数内可以调用其他的const成员函数和非成员函数吗?
- 可以 - 可以
记住一条原则:权限可以被缩小,不可以被放大

inline(内联)

以inline修饰的函数叫做内联函数,编译时C++编译器会调用内联函数的地方展开,减少函数压栈的开销,
提升程序运行时的效率。
1. inline是一种以空间换时间的做法,省去调用函数的额外开销。所以代码很长或者有循环/递归
   的函数不宜使用内联
2. inline内联对于编译器而言只是一个建议,编译器会自动优化,如果定义为inline的函数体内有
   循环/递归等等,编译器优化时会忽略掉内联。
3. inline必须函数定义放在一起,才能成为内联函数,仅将inline放在声明前是不起不作用。
4. 定义在类内的成员函数默认定义为内联函数。
class Date
{
public:
    void Func() //定义在类内部默认为内联函数
    {}
    void Display();
private:
    int _year;//年
    int _month;//月
    int _day;//日
};

inline void Date::Display()     //成员函数定义为内联
{
    cout<<"year:"<<_year<<endl;
    cout<<"month:"<<_month<<endl;
    cout<<"day:"<<_day<<endl;
}
inline void Test()      //全局函数定义为内联
{}

友元函数

友元函数:
在C++中友元函数允许在类外访问类中的任何成员,就像成员函数一样,友元函数用关键字friend说明。
1. 友元函数不是类的成员函数。
2. 友元函数可以通过对象访问所有成员,私有和保护成员也一样。

class Date
{
friend void Display( const Date & d);
private :
int _year ; // 年
int _month ; // 月
int _day ; // 日
};
void Display (const Date& d)
{
cout<<"year:" <<d. _year<< endl ;
cout<<"month:" <<d. _month<< endl ;
cout<<"day:" <<d. _day<< endl ;
}
void Test ()
{
Date d1 ;
Display(d1 );
}
  • 输入输出运算符的重载的友元函数
class Date
{
public:
friend ostream & operator<<(ostream& os,const Date& d);
friend istream & operator>>(istream& is,Date& d);

private:
int _year;  //年
int _month;  //月
int _day;   //日                                                     v
};

ostream& operator<<(ostream& os,const Date& d)
{
    os<<"year:"<<d._year<<endl;
    os<<"month:"<<d._month<<endl;
    os<<"day:"<<d._day<<endl<<endl;
    return os;
}

//cin.operator<<(cin,d1)
istream& operator>>(istream is,Date& d)
{
    cout<<"请分别输入年月日:"<<endl;
    is>>d._year;
    is>>d._month;
    is>>d._day;

    return is;
}
void Test()
{
    Date d1;
    cin>>d1;
    cout<<d1;
}

友元类:

整个类可以是里一个类的友元。友元类的每个成员函数都是另一个类的友元函数,都可以
访问另一个类中的保护或私有数据成员。

class Time
{
//Date是Time的友元,所以Date可以访问Time的所有成员。
friend class Date;
private:
    int _hour;
    int _minute;
    int _second;
};

class Date
{
public:
    void Display()
    {
        cout<<"year:"<<_year<<endl;
        cout<<"month:"<<_month<<endl;
        cout<<"day:"<<_day<<endl;
        //定义为友元类后,可以访问Time类对象的所有成员
        cout<<"hout:"<<_t.hour<<endl;
        cout<<"minute:"<<_t.minute<<endl;
        cout<<"second:"<<_t.second<<endl<<endl;
    }
private:
    int _year;
    int _month;
    int _day;

    Time _t;
};
void Test()
{
    Date d1;
    d1.Display();
}

注意:
友元一定程度上破坏了C++的封装,友元不宜多用,在恰当的地方使用友元。

类的静态成员

-1 类里面static修饰的成员,称为静态类成员。
-2 类的静态成员是该类型的所有对象所共享。

这里写图片描述

静态成员的定义及使用:

class Date
{
    public:
        Date()
        {
            cout<<"Date()"<<endl;
            ++sCount;
        }
        void Display()
        {
            cout<<"year:"<<_year<<endl;
            cout<<"month:"<<_month<<endl;
            cout<<"day:"<<_day<<endl;
        }
        //静态成员函数
        static void PrintCount()
        {
            cout<<"Date count:"<<sCount<<endl;
        }
    private:
        int _year;
        int _month;
        int _day;
    private:
        static int sCount;  //静态成员变量,统计创建时间个数
};

//定义并初始化静态成员变量
int Date::sCount = 0;

void Test()
{
    Date d1,d2;
    //访问静态成员
    Date::PrintCount();
}

注意静态成员函数没有隐含的this指针参数,所以可以使用类型::作用域访问符直接调用静态成员函数。

1. 静态成员函数可以访问非静态成员
2. 非静态成员不可以访问静态成员
  • 构造函数拷贝赋值函数的N种调用情况
class Date
{
public :
Date()
{
cout<<"Date()" <<endl;
}
Date(const Date& d)
{
cout<<"Date(const Date& d)" <<endl;
}
Date& operator =(const Date& d )
{
cout<<"Date& operator=(const Date& d)"<< endl;
return *this ;
}
~ Date()
{
cout<<"~Date()" <<endl;
}
};
// 1.Date 对象做参数传值 & 传引用
void fun1 (Date d) //void fun1(Date& d)
{}
// 2.Date 对象做返回值传值 & 传引用
Date fun2 () // Date& fun2()
{
Date d ;
return d ;
}
// 3.Date 对象做临时返回值传值 &传引用(编译器优化问题)
Date fun3 () // Date& fun3()
{
return Date ();
}
int main ()
{
// 场景
//Date d1;
//fun1(d1);
// 场景
Date d2 = fun2();
// 场景
Date d3 ;
d3 = fun3 ();
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值