Funktionen mit const-Argumenten Überladen (Follow-up)

Dies ist ein Nachfolger desVorherige Frage

Es wurde wirklich kompliziert, also beginne ich einen neuen Thread, um meinen Standpunkt klarer zu machen. (Ich wollte den vorherigen Thread nicht löschen, weil die anderen Jungs, die wertvolles Feedback gaben, die gewonnenen Reputationspunkte nicht verlieren.)

Updated Code: (Entspricht und funktioniert)

#include <iostream>
 using std::cout;

 class Test {
         public:
         Test(){ }   
         int foo (const int) const;
         int foo (int );
 };  

 int main ()
 {   
         Test obj;
         int variable=0;  
         int output;
     do{ 
         output=obj.foo(3);        // Call the const function 
         cout<<"output::"<<output<<std::endl;
         output=obj.foo(variable); // Want to make it call the non const function 
         cout<<"output::"<<output<<std::endl;
         variable++; 
             usleep (2000000);
        }while(1);  
 }   

 int Test::foo(int a)
 {   
    cout<<"NON CONST"<<std::endl;
    a++;
    return a;
 }   

 int Test::foo (const int a) const
 {   
    cout<<"CONST"<<std::endl;
    return a;
 }   

Output (ich bekomme):

NON CONST
output::4
NON CONST
output::1
NON CONST
output::4
NON CONST
output::2
NON CONST
output::4
NON CONST
output::3
NON CONST
output::4
NON CONST
output::4
NON CONST
output::4
NON CONST
output::5

Output (ich wünschte / hatte im Sinn)

CONST
output::3
NON CONST
output::1
CONST
output::3
NON CONST
output::2
CONST
output::3
NON CONST
output::3
CONST
output::3
NON CONST
output::4
CONST
output::3
NON CONST
output::5

Hope ich habe meine Frage besser dargestellt. Ich kenne andere Möglichkeiten. aber ist das möglich.

Antworten auf die Frage(12)

Ihre Antwort auf die Frage