*
no*******@gmail.com:
I have a base class, PadRcv, with virtual functions. User code
will derive from this class and possibly supply it's own functions
to override the base class virtual functions. How can I test that
a user-defined class has overriden the virtual function in the
base class?
I have code like this which compiles with gcc 2.95.4:
Attrib Implicits(PadRcv *obj){
Attrib accum = 0;
PadRcv *padrcv;
if( !obj ) return 0;
char* (PadRcv::*pmfa)(char *) = &PadRcv::kbd;
void (PadRcv::*pmfb)() = &PadRcv::userclose;
if( obj->*pmfa != &PadRcv::kbd ) accum |= ACCEPT_KBD;
if( obj->*pmfb != &PadRcv::userclose ) accum |= USERCLOSE;
return accum;
}
With gcc 3.4.4, I get the following errors:
pad.C: In function `Attrib Implicits(PadRcv*)':
pad.C:27: error: invalid use of non-static member function
pad.C:28: error: invalid use of non-static member function
*** Error code 1
There is plenty of documentation on the net for pointers to member
functions and virtual functions but nothing seems to cover this
particular area.
Note that the first comparision is essentially
obj->*(&PadRcv::kbd) != &PadRcv::kbd
and one does not expect the two sides to have the same types since the
left hand side is in a sense dereferenced.
But it's worse: there's /no/ type in C++ for the result of "->*" and
".*". The only thing you can do with such a result is to use it as the
operand for the function call operator. I.e., apply (...).
One technical solution is to test for the presence of an interface
rather than presence of a virtual function, i.e. using dynamic_cast.
But in general a better approach is to design the base class so that it
doesn't need to know anything about its derived classes, and to not try
to be overly clever or do evil premature micro-optimization.
Now for details.
1. All uppercase for constants is a Java'ism.
Don't do it. Reserve all uppercase for macros.
2. More naming: "Implicits", "kbd" and "userclose" form a set of
names with no apparent naming convention.
3. Even more about names. PadRcv is just as silly as early assembly
language HLT, or Unix creat. Save one keystroke, for what?
4. The Attrib enumeration. In standard C++ you need to cast from
int to Attrib.
5. char* as argument is ungood. Use char const*. Or better,
std::string const&.
6. PadRcv* as argument is ungood. At least use PadRcv const*. But
better use PadRcv const&.
7. Kudos: your code caused MSVC 7.1 to ICE. ;-)
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?