利用const引用/指针重载最佳匹配的问题

C++函数重载与const引用
本文探讨了C++中函数重载机制下const引用参数的调用规则。通过实例说明,当存在非const与const引用重载时,如何选择最佳匹配函数,并解释了为何非const引用可以初始化const引用,反之则不行。
void func(const char &a)
{
	cout<<"non-const";
}
void func(char &a)
{
	cout<<"const";
}
int main()
{
	char p = '1';
	func(p);
	return 0;
}

此时func§调用的是非const的函数,而其实他与const的函数也是匹配的,但不是最佳。如果像下面没有非const函数,则将调用const的函数。


void func(char &a)
{
 cout<<"const";
}
int main()
{
 char p = '1';
 func(p);
 return 0;
}

此时调用的是非const的函数。
因为形参在初始化时,可以用非const的引用对象初始化const引用对象的。反过来不行


//const char ptr = '2';
 //char &a = ptr;//报错
 char ptr = '2';
 const char &a = ptr;//这样时可以的
 
实现一个矩阵类,其声明如下: #include "stdafx.h" #include <iostream> using namespace std; class Matrix; // 矩阵类 class MatrixIndex; // 子矩阵类 // 矩阵类 class Matrix{ private: double *data; // 核心数据,用一维数组存储二维矩阵 int nRow; // 矩阵行数 int nColumn; // 矩阵列数 public: // 构造一个x行y列的矩阵,元素值全零 Matrix(int x, int y); // 构造一个x行y列的矩阵,当type为"zero"时元素值全零, // 为"one"时元素值全一,为"rand"时元素值全随机 Matrix(int x, int y, const char* type); // 复制构造函数 Matrix(const Matrix&amp;m1); // 用临时数组构造矩阵,例如当输入为{1,2,3}时构造一个 // 1*3的矩阵 Matrix(std::initializer_list<int> vector); // 用子矩阵构造一个矩阵,子矩阵仅保存某矩阵的引用,此 // 处将子矩阵引用的数据进行深拷贝以构造一个全新矩阵, // 这使得子矩阵可以作为右值同矩阵一样使用 Matrix(const MatrixIndex&amp; index); // 销毁数组data ~Matrix(); // 用矩阵构造一个子矩阵,子矩阵仅保存某矩阵的引用,例 // 如m1={1,2}且m2={3,4},则m(m1,m2)构造以m的第1,2行和 // 第3,4列构造的子矩阵,子矩阵仅保存源矩阵的引用 MatrixIndex operator()(const Matrix&amp;m1, const Matrix&amp;m2); // 重载等号运算符,实现m=m1 Matrix&amp; operator=(const Matrix &amp;m1); // 重载等号运算符,将m的所有元素更改为a Matrix&amp; operator=(double a); // 重载等号运算符,将m更改为行向量vector,此时m中的元 // 素数目需要和vector中的元素数目相同 Matrix&amp; operator=(std::initializer_list<double> vector); // 重载加号运算符,实现m1+m2,矩阵大小需满足运算规则 friend Matrix operator+(const Matrix &amp;m1, const Matrix &amp;m2); // 重载减号运算符,实现m1-m2,矩阵大小需满足运算规则 friend Matrix operator-(const Matrix &amp;m1, const Matrix &amp;m2); // 重载乘号运算符,实现m1*m2,矩阵大小需满足运算规则 friend Matrix operator*(const Matrix &amp;m1, const Matrix &amp;m2); // 重载<<运算符,实现cout<<m1打印矩阵内容 friend ostream&amp; operator<<(ostream &amp;out, const Matrix &amp;m1); friend class MatrixIndex; }; // 子矩阵类,其中每个元素为指向某矩阵元素的指针 class MatrixIndex{ private: const Matrix&amp; source; // 子矩阵对应的源矩阵 const Matrix&amp; rows; // 子矩阵对应源矩阵的行 const Matrix&amp; columns; // 子矩阵对应源矩阵的列 public: // 用矩阵构造一个子矩阵,子矩阵仅保存矩阵source的 // 第rows行和第columns列的引用 MatrixIndex(const Matrix&amp;source, const Matrix&amp;rows, const Matrix&amp; columns) :source(source), rows(rows), columns(columns){} // 重载等号运算符,实现m(...)=m1,此时左右元素数目 // 需相同,这使得子矩阵可以作为左值同矩阵一样使用 MatrixIndex&amp; operator=(const Matrix &amp;m1); // 重载等号运算符,将m(...)的所有元素更改为a MatrixIndex&amp; operator=(double a); // 重载等号运算符,将m(...)更改为行向量vector,此 // 时左右元素数目需相同 MatrixIndex&amp; operator=(std::initializer_list<double> vector); friend class Matrix; };
06-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值