--- In [email protected], saurabh jain <hundredr...@...> wrote:
>
> Hi,
>   Can anyone tell what is supposed to be the behaviour for a function which 
> is declared as virtual in a base class and non-virtual in derived?
> Specially pertaining to cases when you cast the derived object into a base 
> class object and call the function.
> 
> For example:
> 
> class Base {
>   public:
>       virtual void foo() { cout << " I am Base foo \n"; }
> };
> 
> class Derived : public Base {
>   public:
>      void foo() { cout << " I am Derived foo\n";}
> };
> 

For my first question this is what I understood from the standard:
"If a virtual member function vf is declared in a class Base and in a class 
Derived, derived directly or
indirectly from Base, a member function vf with the same name and same 
parameter list as Base::vf is
declared, then Derived::vf is also virtual (whether or not it is so declared) 
and it overrides95)
__________________
95) A function with the same name but a different parameter list (13) as a 
virtual function is not necessarily virtual and does not over10.3
Virtual functions Derived classes 10– 7
Base::vf."


> And to extend the question what if another class Derived2 is derived from 
> class "Derived" with the function foo being declared as non-virtual. 
> 
For the second question :

"[Note: a virtual member function does not have to be visible to be overridden, 
for example,
struct B {
virtual void f();
};
struct D : B {
void f(int);
};
struct D2 : D {
void f();
};
the function f(int) in class D hides the virtual function f() in its base class 
B; D::f(int) is not a virtual
function. However, f() declared in class D2 has the same name and the same 
parameter list as
B::f(), and therefore is a virtual function that overrides the function B::f() 
even though B::f() is
not visible in class D2. ]
" 


> Thanks and Regards,
> Saurabh
> 
>  $ make war 
> make: *** No rule to make target `war&#39;. Try `peace&#39; instead
>


Reply via email to