As you will see, I am not very confident on my understanding of what happens when mixing pointers const and overload. So following on questions such as this one, I tried to explicit all cases in the following piece of code.
My question is : did I miss some cases of interactions between addresses, pointers, const pointers, and the various prototypes that may overload each other ?
//g++ 7.4.0
#include <iostream>
using namespace std;
// ---------- which legal overloads ?
/*
void f_012a( int* pi ){ cout << "pi"; }
//void f_012a( int * const pi ){ cout << "pci"; } //error: redefinition of ‘void f_012a(int*)’
void f_012a( const int * pi ){ cout << "pci"; }
//void f_012a( const int * const pi ){ cout << "cpci"; } //error: redefinition of ‘void f_012a(const int*)’
*/
// ---------- overloads declaration order impact ?
void f_01( int* pi ) { cout << "pi"; }
void f_01( const int * pi ){ cout << "pci"; }
void f_10( const int * pi ){ cout << "pci"; }
void f_10( int* pi ) { cout << "pi"; }
int main(){
cout << "/** trace\n";
{
cout << "-------- pointers and const\n";
int* pi = new int(1);
int * const pci = new int(2);
const int * cpi = new int(3);
const int * const cpci = new int(4);
cout << "pi -> " << *pi << " / pci -> " << *pci << " / cpi -> " << *cpi << " / cpci -> " << *cpci << "\n";
*pi = 5;
cout << "pi -> " << *pi << " / pci -> " << *pci << " / cpi -> " << *cpi << " / cpci -> " << *cpci << "\n";
pi = new int(6);
cout << "pi -> " << *pi << " / pci -> " << *pci << " / cpi -> " << *cpi << " / cpci -> " << *cpci << "\n";
*pci = 7;
cout << "pi -> " << *pi << " / pci -> " << *pci << " / cpi -> " << *cpi << " / cpci -> " << *cpci << "\n";
//pci = new int(8); //error: assignment of read-only variable ‘pci’
//*cpi = 8;
cpi = new int(8); //error: assignment of read-only location ‘* cpi’
cout << "pi -> " << *pi << " / pci -> " << *pci << " / cpi -> " << *cpi << " / cpci -> " << *cpci << "\n";
//*cpci = 9; //error: assignment of read-only location ‘* cpi’
//cpci = new int(9); //error: assignment of read-only variable ‘cpci’
}
{
cout << "-------- pointers, const, and overload\n";
int i = 1;
int* pi = new int(2);
int * const pci = new int(3);
const int * cpi = new int(4);
const int * const cpci = new int(5);
cout << " i : " << i << " / pi -> " << *pi << " / pci -> " << *pci << " / cpi -> " << *cpi << " / cpci -> " << *cpci << "\n";
cout << "var\tf_01\tf_10\n";
cout << "&i\t"; f_01(&i); cout<<"\t"; f_10(&i); cout<<"\n";
cout << "pi\t"; f_01(pi); cout<<"\t"; f_10(pi); cout<<"\n";
cout << "pci\t"; f_01(pci); cout<<"\t"; f_10(pci); cout<<"\n";
cout << "cpi\t"; f_01(cpi); cout<<"\t"; f_10(cpi); cout<<"\n";
cout << "cpci\t"; f_01(cpci); cout<<"\t"; f_10(cpci); cout<<"\n";
}
cout << "*/\n";
return 0;
}
/** trace
-------- pointers and const
pi -> 1 / pci -> 2 / cpi -> 3 / cpci -> 4
pi -> 5 / pci -> 2 / cpi -> 3 / cpci -> 4
pi -> 6 / pci -> 2 / cpi -> 3 / cpci -> 4
pi -> 6 / pci -> 7 / cpi -> 3 / cpci -> 4
pi -> 6 / pci -> 7 / cpi -> 8 / cpci -> 4
-------- pointers, const, and overload
i : 1 / pi -> 2 / pci -> 3 / cpi -> 4 / cpci -> 5
var f_01 f_10
&i pi pi
pi pi pi
pci pi pi
cpi pci pci
cpci pci pci
*/