This is a follow up of the Previous Question
It got really complicated so I am starting a new thread to make my point clearer.( Didnt want to delete the previous thread because the other guys who gave valuable feedback dont not loose the reputation points they gained)
Updated Code: (Complies and Works)
#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 (I get):
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 (I desired/had in mind)
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 I have presented my question better. I know other ways to do it. but is this possible.
Yep, I can't make it call the const version - unless I do this:
No matter what parameter is passed in, if it can call the non-const, it will. If you have a const object, it calls the const version of the function.
Interesting.
The call of const (or non-const) function doesn't depend of the constness of the parameters but only of the constness of the called object (in our case
obj
). Overload needs different type and (non-const const) are not, so I don't think you can overload as you are doing it. (that work because you are defining a const and a non-const methods but that's not overloading.) To persuade yourself, try to remove the const at the end of your declaration to see if you are allowed to declareYou'll get an error.
In the second case you think foo is expecting a
const int
as argument but not. const is tied toa
not toint
. So what it says is foo expect anint
, you could refer it usinga
and that will be const : you are not allowed to modifya
. That's why the const (of a parameter) doesn't appear in the function signature (that would be different for a reference).The const outside the function refers to object called , so that's part of the signature
(I'm not 100% sure about the type syntax , so don't comment on it, that's just to give an idea)
As you can see the
const
get removed witha
. You can also write itint const a
, even if not the standard way to do it, it's perfectly legal.By the way , your code will never do what you are expected, you should use a reference to an int to modify it
Just a clarification. However, we are allowed to overload pointers with and without const arguments in functions, right?
As in,
How is this different?
In C++, the function signatures
and
are considered to be complete identical.
The reason that the
const
on the parameter is disregarded is because it can not affect the caller in any way. As the parameter is passed by value, a copy is made of the value provided by the caller. To the caller, it does not matter in any way if the called function can change that copy or not. For this reason, C++ ignores a top-levelconst
-qualification on function parameters (top-levelconst
can not occur if passing a reference), and goes even as far thatint foo(int);
is considered a correct prototype for the functionIn short, it is impossible in C++ to overload a function on the constness of (value) function parameters. To get the output you want, you could consider using a non-const reference parameter for your non-const overload.
Okay, this is how it works:
When you call a function, the value of the parameters are passed to the function. If you pass a value, then this value will be what the function gets. If you pass a pointer, then your function will get a pointer.
When your function receives the argument, all it sees is an integer value. It cannot determine where this value came from, and cannot be static or non static. These properties belong to pointers, which indicate whether or not you can change the value pointed to by the pointers, not the pointers themselves.
Just for good measure, all these function calls look IDENTICAL to the function that recieves them:
So to answer your question, what you are doing is not possible.
EDIT: To change the variable, overload the function with an int, and an int pointer. By passing the address of an int value to the function, your function can change it.
Convert you
const int
to a string, overload yourfoo
with a string and convert back...Feel free to destroy my answer and the comments.