Think about the next code :

#include <iostream>
using namespace std;

class A
    virtual int f() {cout <<"A:: f()\n"; return 1;}
    virtual void f(int) {cout <<"A:: f(int)\n";}
    virtual void g() {cout <<"A::g()\n";}

class B3 : public A
    void f() {cout <<"B3::f ()\n";}

int main()
    return 0;

It creates the next error :

..\main.cpp:17: error: conflicting return type specified for 'virtual void B3::f()'
..\main.cpp:9: error:   overriding 'virtual int A::f()'

why ? within the worst situation I'd think I'd come with an Hiding situation , but rather I recieve compilation error regarding A's virtual int f() {cout <<"A:: f()\n"; return 1;}

thanks ,Ronen

Don't confuse overriding with hiding. You override virtuals.

Your class definition is the same as:

class B3 : public A
    virtual void f() {cout <<"B3::f ()\n";}

When a function declared virtual, it'll remain virtual for those classes drawing out of your base class, no matter whether you clearly declare it virtual or otherwise. Therefore you are attempting to override an online function and altering its return type, that is illegal in C++. When the function weren't virtual, you'd just be hiding the bottom class implementation, therefore altering the return type applies. There'd be no ambiguity because the compiler knows where you can call the function from and what return type to anticipate.

However, consider getting:

A* a;

An amount a-f() return? a is really a pointer to some, but could indicate an item of type B3. Therefore it either returns an int or does not return anything. Begin to see the ambiguity here?

Rather, no polymorphism involved,

A a;

will cal f from the, just like b3.f would call f from B3. Overall, overriding base class functions suggests keeping exactly the same return type. If you wish to produce a new function having a different return type, change its signature (either its title or its parameters - or both).

Anyway, you should not be also carrying this out... Why would you need to possess a function with similar title with no parameters return various things? Wouldn't adding another function become more readable?

You'd have hiding if f() might have were built with a different parameter list, or otherwise declared as virtual around the base class. Within the former situation, since over-loading does not mix inheritance edges, A's f could have been hidden. But this isn't the situation, because you have f() on classes, which only differ in exchange value. Return values covariance may be the only difference permitted, and also, since it isn't the situation (void doesn't inherit from int), you receive the mistake.