Detecting classes with virtual functions

N

News Admin

I have a bunch of classes, instances of which that need to live in shared
memory and be accessed by multiple processes. This means that they cannot
have a vtable as the addresses in it will be in the address space of the
process that originally created the shared memory and therefore unavailable
to the other processes that may wish to use these instances of the classes.
Every so often I forget this and introduce a virtual function causing a
period of brow-furrowing until I recall the prohibition.
What I want to know is, is there a (preferably compile-time) method of
determining if a given class has virtual functions, so I can catch this
before the brow-furrowing part.
 
M

Marco Manfredini

News said:
I have a bunch of classes, instances of which that need to live in
shared memory and be accessed by multiple processes. This means that
they cannot have a vtable as the addresses in it will be in the
address space of the
process that originally created the shared memory and therefore
unavailable to the other processes that may wish to use these
instances of the classes.

Have you thought of a platform specific solution? Like putting the
classes into a shared library which is configured with a reserved load
address?
Every so often I forget this and introduce a
virtual function causing a period of brow-furrowing until I recall the
prohibition. What I want to know is, is there a (preferably
compile-time) method of determining if a given class has virtual
functions, so I can catch this before the brow-furrowing part.

There is no known generally acceptable way to detect polymorphic classes
at compile time. There are implementation dependant ways to do it, like
this one, which misuses knowledge of the compilers behaviour:

template<class T>
struct has_virtual
{
struct X : T
{
template<class A>
X(A a=A()) {}
virtual ~X();
};
static const bool value = (sizeof(X)==sizeof(T));
};

has_virtual<X>::value is true, if X has a) virtual members functions or
b) virtual base classes, at least where I've checked it.

Greetings
Marco
 
D

David Baraff

News Admin said:
What I want to know is, is there a (preferably compile-time) method of
determining if a given class has virtual functions, so I can catch this
before the brow-furrowing part.

Sure, but non-portable. Still, have never seen this fail:

template <class T>
struct PolymorphicTester : public T {
PolymorphicTester();
virtual ~PolymorphicTester();
};

/*
* See if T changes size
* when we add a virtual function to it. If it doesn't get bigger,
then it was
* already polymorphic.
*/

#define IS_POLYMORPHIC(T) (sizeof(PolymorphicTester< T >) ==
sizeof(T))

Note that IS_POLYMORPHIC(T) is a compile-time constant, so you can
stick it into a template and, for example, make it fail with a
compile-time assertion, etc (see concept_checking in the boost
libraries for an example).

Still, *somebody* has to go to the trouble of, for each class you're
worried about, making sure that the compile-time assertion exists
someplace in the code.
 
J

John Torjo

News Admin said:
I have a bunch of classes, instances of which that need to live in shared
memory and be accessed by multiple processes. This means that they cannot
have a vtable as the addresses in it will be in the address space of the
process that originally created the shared memory and therefore unavailable
to the other processes that may wish to use these instances of the classes.
Every so often I forget this and introduce a virtual function causing a
period of brow-furrowing until I recall the prohibition.
What I want to know is, is there a (preferably compile-time) method of
determining if a given class has virtual functions, so I can catch this
before the brow-furrowing part.

Why would you need such a thing?
Anyway, you can use DCOM objects (have an .exe server out-of-proc) -
for shared inter-comunication. And it's portable (across Windows
platforms), and you woudn't need to worry about any vtable, etc.

Best,
John

John Torjo
Freelancer
-- (e-mail address removed)

Contributing editor, C/C++ Users Journal
-- "Win32 GUI Generics" -- generics & GUI do mix, after all
-- http://www.torjo.com/win32gui/

Professional Logging Solution for FREE
-- http://www.torjo.com/code/logging.zip (logging - C++)
-- http://www.torjo.com/logview/ (viewing/filtering - Win32)
-- http://www.torjo.com/logbreak/ (debugging - Win32)
(source code available)
 
R

red floyd

John Torjo wrote:
\
Why would you need such a thing?
Anyway, you can use DCOM objects (have an .exe server out-of-proc) -
for shared inter-comunication. And it's portable (across Windows
platforms), and you woudn't need to worry about any vtable, etc.

Who said he was running Windows?
 
D

David Abrahams

News Admin said:
I have a bunch of classes, instances of which that need to live in shared
memory and be accessed by multiple processes. This means that they cannot
have a vtable as the addresses in it will be in the address space of the
process that originally created the shared memory and therefore unavailable
to the other processes that may wish to use these instances of the classes.
Every so often I forget this and introduce a virtual function causing a
period of brow-furrowing until I recall the prohibition.
What I want to know is, is there a (preferably compile-time) method of
determining if a given class has virtual functions, so I can catch this
before the brow-furrowing part.

Use boost::is_polymorphic<T>

see http://www.boost.org/libs/type_traits

HTH,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top