2

I have following code:

//base
class base  
{  
public :  
  virtual void func(){  
    cout<<"Base::func"<< endl;  
}  
};  

class derived : public base  
{  
public :  
     static void func() // overridden function
{  
    cout<<"derived::func"<< endl;  
}  
};  

Here, I have overridden the func() in derived class as static. Why compiler won't allow this?

apero
  • 95
  • 7
  • for once because the language doesn't allow it. Then because it wouldn't make any sense. – bolov Feb 02 '15 at 21:48
  • 2
    It doesn't make sense, like, at all. `func` in base is called in the context of a particular instance of `base`. `func` from `derived` is to be called always as `derived::func`, without any object - how can it "override" the former? – BartoszKP Feb 02 '15 at 21:48
  • @Mark: That dupe is nearly even relevant for this question. Trouble is, nearly a hit is still a miss. – Deduplicator Feb 02 '15 at 22:05
  • The answer is, because a static method and a non-static methot simply aren't all that similar. What I wonder though is why it does not allow hiding it. – Deduplicator Feb 02 '15 at 22:08
  • 1
    @Deduplicator There's a possibility I misunderstood the question. The OP used "overridden" rather than "hiding" or "replacing" so I assumed they wanted the virtualness to fall through to the derived class, which is answered by my linked duplicate. – Mark B Feb 02 '15 at 22:12
  • @Mark: The base-class-function is not `static`, which means the explanation in the dupe does not really fit. – Deduplicator Feb 02 '15 at 22:15

0 Answers0