Freigeben über


ANSWER: POP QUIZ: What’s wrong with this code – part 2

This was a trick question.  There is actually nothing wrong with this code.  Take a look at this in regards to this type of code:

9.3/9: A member function can be declared (but not defined) using a typedef for a function type.  The resulting member function has exactly the same type as it would have if the function declarator were provided explicitly"

You can see this and the other rules at: Classes 9.3

Yes this is a very simple example of using a function pointer, and it doesn't do much, but this is legitimate code.  There are certainly things you can do to make it better, but that is besides the point.

Hope you enjoyed trying to figure it out.

Comments

  • Anonymous
    May 16, 2008
    I beg to differ. The syntax is not wrong, I agree. This was my answer:

HeartattacK said on May 15, 2008 12:59 PM:

  1. Method is virtual, yet access is private. A child can not use the default implemention. Furthermore, as m_p is also private, the child can't provide  an implementation that uses m_p.
  2. m_p has not been set anywhere, yet it is being compared with. The error is logical. A virtual function that's private should not be virtual. And the private pointer is never initialized. That said, if the focus was on syntax, then you're right :)
  • Anonymous
    May 16, 2008
    HeartattacK, I agree with both of those and you are right that those are problems in the code.  Good catch.  I was mostly concerned with the Syntax which is why I gave the answer I did.  But those are good answers as well.