[問題] 參照變數為什麼不能宣告為const?
遇到的問題: (題意請描述清楚)
在螞蟻本5版p719(原文書)的參照parameter為什麼不能宣告成const
我們知道pointer可以宣告成const
為什麼reference varable不能宣告成const?
希望得到的正確結果:
程式跑出來的錯誤結果:
若問題------->改為void virtualViaReference( const Employee & const);
多了後面的const,會出現error message如下
23 C:\dev\fig13_23.cpp `const' qualifiers cannot be applied to `const
Employee&'
開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux)
Dev-C++
有問題的code: (請善用置底文標色功能)
// Fig. 13.23: fig13_23.cpp
// Processing Employee derived-class objects individually
// and polymorphically using dynamic binding.
#include <iostream>
using std::cout;
using std::endl;
using std::fixed;
#include <iomanip>
using std::setprecision;
#include <vector>
using std::vector;
// include definitions of classes in Employee hierarchy
#include "Employee.h"
#include "SalariedEmployee.h"
#include "HourlyEmployee.h"
#include "CommissionEmployee.h"
#include "BasePlusCommissionEmployee.h"
void virtualViaPointer( const Employee * const ); // prototype
*[1m問題---------> void virtualViaReference( const Employee &); // prototype*[m
int main()
{
// set floating-point output formatting
cout << fixed << setprecision( 2 );
// create derived-class objects
SalariedEmployee salariedEmployee(
"John", "Smith", "111-11-1111", 800 );
HourlyEmployee hourlyEmployee(
"Karen", "Price", "222-22-2222", 16.75, 40 );
CommissionEmployee commissionEmployee(
"Sue", "Jones", "333-33-3333", 10000, .06 );
BasePlusCommissionEmployee basePlusCommissionEmployee(
"Bob", "Lewis", "444-44-4444", 5000, .04, 300 );
cout << "Employees processed individually using static binding:\n\n";
// output each Employee旧 information and earnings using static binding
salariedEmployee.print();
cout << "\nearned $" << salariedEmployee.earnings() << "\n\n";
hourlyEmployee.print();
cout << "\nearned $" << hourlyEmployee.earnings() << "\n\n";
commissionEmployee.print();
cout << "\nearned $" << commissionEmployee.earnings() << "\n\n";
basePlusCommissionEmployee.print();
cout << "\nearned $" << basePlusCommissionEmployee.earnings()
<< "\n\n";
// create vector of four base-class pointers
vector < Employee * > employees( 4 );
// initialize vector with Employees
employees[ 0 ] = &salariedEmployee;
employees[ 1 ] = &hourlyEmployee;
employees[ 2 ] = &commissionEmployee;
employees[ 3 ] = &basePlusCommissionEmployee;
cout << "Employees processed polymorphically via dynamic binding:\n\n";
// call virtualViaPointer to print each Employee's information
// and earnings using dynamic binding
cout << "Virtual function calls made off base-class pointers:\n\n";
for ( size_t i = 0; i < employees.size(); i++ )
virtualViaPointer( employees[ i ] );
// call virtualViaReference to print each Employee's information
// and earnings using dynamic binding
cout << "Virtual function calls made off base-class references:\n\n";
for ( size_t i = 0; i < employees.size(); i++ )
virtualViaReference( *employees[ i ] ); // note dereferencing
system("pause");
return 0;
} // end main
// call Employee virtual functions print and earnings off a
// base-class pointer using dynamic binding
void virtualViaPointer( const Employee * const baseClassPtr )
{
baseClassPtr->print();
cout << "\nearned $" << baseClassPtr->earnings() << "\n\n";
} // end function virtualViaPointer
// call Employee virtual functions print and earnings off a
// base-class reference using dynamic binding
void virtualViaReference( const Employee & const baseClassRef )
{
baseClassRef.print();
cout << "\nearned $" << baseClassRef.earnings() << "\n\n";
} // end function virtualViaReference
補充說明:
一般的reference variable是不是也不能宣告成const呢?
thanks a lot
--
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.137.29.80
→
01/23 03:47, , 1F
01/23 03:47, 1F
推
01/23 03:53, , 2F
01/23 03:53, 2F
→
01/23 04:06, , 3F
01/23 04:06, 3F
推
01/23 04:22, , 4F
01/23 04:22, 4F
推
01/23 04:24, , 5F
01/23 04:24, 5F
→
01/23 04:24, , 6F
01/23 04:24, 6F
推
01/23 19:54, , 7F
01/23 19:54, 7F