Check for existence of virtual function

Hi, is it possible to know if a class has virtual function definition from the object of it? given that I do not have access to the source code of it...
How could you have an instance of the object without having its declaration somewhere (like in a .h file)?
Last edited on
closed account (S6k9GNh0)
I think he means, for instance, in a plugin system. What if that plugin didn't implement a function?
If that is true, it depends. You'll have to explain more about what you're doing.
No, there is no compilation error in the class definition. question is as follows -

class A
{
public:
virtual void foo(){};
};

int main()
{
A a;
// how can i check object "a" has "a virtual function" ?
}

when a class has any virtual function, it would have a vtable associated with it. can i check in main if that vtable exists? if so, what is the size of that vtable? mere existence of vtable would indicated existaene of a virtual function in a class definition.
A class containing at least one virtual function should have its destructor declared virtual as well. Boost provides the has_virtual_destructor<> type trait

http://www.boost.org/doc/libs/1_46_1/libs/type_traits/doc/html/boost_typetraits/reference/has_virtual_destructor.html

However, not all compilers support it. Read the section on Compiler Compatibility.

Topic archived. No new replies allowed.