So, in a base class I have method foo return type say.. int. Then in a derived class I have method foo with return void. I though the types and name created a signature. IE you can create different foo methods with different input and return types. Is this no longer the case? Or, is it the case that just input types can be different?
jimLee:
Has it always been that way? I'd think I'd have run into this years before if it were the case.
-jim lee
Well yeah. If you think about it from the point of view of coding your own compiler.....
If you hit a function call and there are two different versions of the same function defined with different return types, then how could you be certain as to which function the person, who wrote the code (you are trying to compile), meant.
In contrast it is obvious if the list of parameters is different between functions with the same name.
As long as you are not trying to override the method from base class, your derive class can have the method with the same name but different return type;
arduino_new:
As long as you are not trying to override the method from base class, your derive class can have the method with the same name but different return type;
I am sure I have done this before accidentally and it has given me the same compile error?
Or perhaps it was with stand alone functions not part of a class.
At any rate it is bad programming practice to do it, because it will confuse the hell out of you when you come back to your code 6 months later.
The correct way would be to use pure virtual functions in the base class and override and implement them in the derived class.
For different return types and same parameter list you should use a different function name.
That's OK. I made them have the same return types and all is happy.
It was a pop() function for a queue. The base class wanted to hand back the popped off node and I just wanted to quietly dispose of it in the derived class. Now they both work the same.
boylesg:
You can't have functions with the same name but different return types.
Same name with different parameters is OK though.
Actually you can. The important thing is that you can't overload a function and change only the return type. If you want a different return type, you have to change the argument list as well so there's no ambiguity when the compiler needs to pick which function to use.