funções com argumentos const Sobrecarga (Acompanhamento)
Este é um acompanhamento doPergunta anterior
Ficou realmente complicado, então estou iniciando um novo tópico para tornar meu argumento mais claro (não queria excluir o tópico anterior porque os outros caras que deram um feedback valioso não perdem os pontos de reputação que obtiveram)
Código atualizado: (Conformidade e Obras)
#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;
}
Saída (recebo):
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
Saída (eu desejava / tinha em mente)
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
Espero ter apresentado minha pergunta melhor. Eu conheço outras maneiras de fazer isso. mas isso é possível.