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));
}
相关文章推荐
- C++对拷贝构造及默认成员函数的优化处理
- 拷贝构造,深度拷贝,关于delete和default相关的操作,explicit,类赋初值,构造函数和析构函数,成员函数和内联函数,关于内存存储,默认参数,静态函数和普通函数,const函数,友元
- 号)、sex(性别)、birthday(出生日期)、id(身份证号)等等。其中“出生日期”定义为一个“日期”类内嵌子对象。用成员函数实现对人员信息的录入和显示。要求包括:构造函数和析构函数、拷贝构造函
- 类的基本成员函数实现(构造,拷贝构造,拷贝赋值,移动构造,移动赋值,析构)
- 拷贝构造函数,深拷贝,大约delete和default相关业务,explicit,给定初始类,构造函数和析构函数,成员函数和内联函数,关于记忆储存,默认参数,静态功能和正常功能,const功能,朋友
- 类的默认成员函数,构造函数,拷贝构造,析构函数,赋值运算符重载
- 【C/C++学院】0819-/类的成员函数与const-mutable /构造与析构/拷贝构造deletedefault以及深浅拷贝/静态成员函数成员变量类在内存的存储默认参数/友元类以及友元函数
- const&&运算符的内联&静态成员&友元&N中构造拷贝构造的优化
- virtual与构造/析构函数(内联函数,构造函数,静态成员函数不能为virtual函数)
- C++拷贝构造的优化
- 构造 和 拷贝构造 的优化处理
- C++ Primer 学习笔记_23_函数(续2) --局部对象、内联函数、类的成员函数
- 内联函数 与 成员函数
- C++ 了解C++默默编写并调用哪些函数(构造 析构 拷贝构造 拷贝赋值)
- 一个Asp 类的模块(成员变量、成员函数、构造析构函数)
- 实现一个string类,包括构造、析构、拷贝构造及operator= 函数
- 在operator=或拷贝构造中对所有数据成员赋值
- 为什么内联函数,构造函数,静态成员函数不能为virtual函数?
- c++将构造与非成员函数虚化的方法
- 为什么内联函数,构造函数,静态成员函数不能为virtual函数