您的位置:首页 > 其它

1.const成员函数 2.内联函数 3.友元 4.static成员 5.N中构造拷贝构造的优化

2018-04-04 15:27 344 查看

1.const成员函数 

const修饰成员函数:
在成员函数后面加const,表示的就是用const修饰this指针所指向的对象,它保证了调用这个const修饰的成员函数时不会改变调用对象的值。
例:
#include<iostream>
#include<stdlib.h>
using namespace std;
class Date
{
public:
Date(int year = 1990, int month = 1, int day = 1)
: _year(year)
, _month(month)
, _day(day)
{

}
void show()const
{
cout << _year << '-' << _month << '-' << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d(2018,3,31);
d.show();
system("pause");
return 0;

}

                                            编译器对const成员函数的处理

                                      


思考题:
1.const对象可以调用const成元函数和非const成员函数吗?
答:const对象可以调用const成元函数不可以调用非const成员函数。
2.非const对象可以调用const成元函数和非const成员函数吗?
答:对
3.const函数可以调用const成元函数和非const成员函数吗?
答:const函数可以调用const成元函数不可以调用非const成员函数。
4.非const对象函数可以调用const成元函数和非const成员函数吗?
答:对

2.inline(内联)

内联函数的定义:
以inline修饰的函数叫做内联函数,编译时c++编译器会在使用内联函数的地方展开,没有函数压栈的开销,内联函数提高程序运行的效率.
内联函数的特点:
1.inline是一种以空间换时间的做法,省去了函数栈帧的开销,但是函数中如果代码很长或者有递归/循环时不适宜使用内联
2.inline对于编译来说只是一个建议,编译器会自动优化,如果代码很长或者有递归/循环则编译器会自动忽略内内联的建议.
3.inline只能和函数的定义放在一起才能有效,如果将inline和函数的声明放在一起是不起作用的.
4.定义在类内的成员函数默认是内联函数.
例:
#include<iostream>
#include<stdlib.h>
using namespace std;
class Date
{
public:
Date(int year = 1990, int month = 1, int day = 1)//默认内联函数
: _year(year)
, _month(month)
, _day(day)
{
}
void show();

private:
int _year;
int _month;
int _day;
};
inline void Date:: show()//成员函数定义为内联函数
{
cout << _year << '-' << _month << '-' << _day << endl;
}
inline void Test()//全局函数定义为内联函数
{}
int main()
{
Date d(2018,3,31);
d.show();
system("pause");
return 0;
}

面试题:
为什么c++建议用const/inline/枚举代替宏
宏的优点:
1.增强代码的复用性
2.提高性能
宏的缺点:
1.宏不方便调试(因为宏在预编译的时候会进行宏替换)
2.导致代码可读性差,可维护性差,容易误用.
3.没有安全类型的检查.

 3.友元

友元函数:在c++中友元函数允许在类外访问类中的任何成员,就想成员函数一样.友元的关键词是friend.
1.友元函数不是类的成元函数.
2.友元函数可以通过对象访问所有成员,私有和保护成员也一样.
例:
#include<iostream>
#include<stdlib.h>
using namespace std;
class Date
{
public:
Date(int year = 1990, int month = 1, int day = 1)//默认内联函数
: _year(year)
, _month(month)
, _day(day)
{
}
friend void show(const Date& d);
private:
int _year;
int _month;
int _day;
};
void show(const Date& d)
{
cout << "year" << d._year << endl;
cout << "month" << d._month << endl;
cout << "day" << d._day << endl;
}
int main()
{
Date d(2018,3,31);
  system("pause");
return 0;

}

友元类

整个类可以是另一个类的友元,友元类的每个成元函数都是另一个类的友元函数,都可以访问另一个类中的私有和保护成员.
例:
#include<iostream>
#include<stdlib.h>
using namespace std;
class Time
{
friend class Date;
private:
int _hour;
int _minute;
int _second;
};
class Date
{
public:
void show()
{
cout << "year" << _year << endl;
cout << "month" <<_month << endl;
cout << "day" << _day << endl;
cout << "hour" << t._hour << endl;
cout << "minute" << t._minute << endl;
cout << "second" << t._second << endl;
}
private:
int _year;
int _month;
int _day;
Time t;
};
int main()
{
Date d;
d.show();
system("pause");
return 0;
}

注:友元在一定程度上破坏了封装性所以尽量少用;
<<和>>d的运算符重载
#include<iostream>
#include<stdlib.h>
using namespace std;
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;
};
ostream& operator<< (ostream& os, const Date& d)
{
os << "year" << d._year << endl;
os << "month" << d._month << endl;
os << "day" << d._day << endl;
return os;
}
istream& operator>> (istream& is,  Date& d)
{
cout << "分别输入年月日" << endl;
is >> d._year;
is >> d._month;
is >> d._day;
return is;
}

int main()
{
Date d;
cin >> d;
cout << d;
system("pause");
return 0;
}

4.类的静态成员

1.类里面用static修饰的成员为静态成员;
2.类的静态成员是该类型的所有对象所共享;
例:
#include<iostream>
#include<stdlib.h>
using namespace std;
class Date
{
public:
Date()
{
cout << "Date()" << endl;
++sCount;
}
void show()
{
cout << "year" << _year << endl;
cout << "month" <<_month << endl;
cout << "day" << _day << endl;
}
//静态成员函数
static void PrintfCount()
{
cout << "Date cout   " << sCount << endl;
}
private:
int _year;
int _month;
int _day;
private:
   static int sCount;//静态成员,统计创建时间的个数
};
//定义并初始化静态变量
int Date:: sCount = 0;
int main()
{
Date d1,d2;
Date::PrintfCount();//访问静态成员
system("pause");
return 0;
}

注:静态成员函数没有隐含this指针,所以可以使用类型::作用域符访问直接调用静态成员函数;
问题:
1.静态成员函数可以访问非静态的成员吗?
答:不可以(因为静态成员函数没有this指针)
2.非静态成员函数可以访问静态的成员吗?
答:可以
构造函数拷贝赋值函数的N种调用情况
#include<iostream>
#include<stdlib.h>
using namespace std;
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;
}
};
//Date做参数传值      &传引用
void fun1(Date d)//void fun1(Date& d)
{}
//Date对象做返回值传值     &传引用
Date fun2()//Date fun2()
{
Date d;
return d;

// Date对象做临时值返回值传值     &传引用(编译器优化问题)
Date fun3(Date d)//Date fun3(Date& d)
{
return Date();
}
int main()
{
Date d1;
fun1(d1);
Date d2=fun2();
Date d3;
d3 = fun3(d3);
system("pause");
return 0;
}

常见题:
Test1中调用了_2_次AA的拷贝构造函数,_1__次AA的赋值运算符函数的重载。 
Test2中调用了_2__次AA的拷贝构造函数,__0_次AA的赋值运算符函数的重载。 
Test3中调用了_3__次AA的拷贝构造函数,_0__次AA的赋值运算符函数的重载。 
class AA 
{}; 
AA f (AA a) 

return a ; 

void Test1 () 

AA a1 ; 
a1 = f(a1); 

void Test2 () 

AA a1 ; 
AA a2 = f(a1); 


void Test3 () 

AA a1 ; 
AA a2 = f(f(a1)); 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐