[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
> 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
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.
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
-- jo...@torjo.com
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)
Who said he was running Windows?
Use boost::is_polymorphic<T>
see http://www.boost.org/libs/type_traits
HTH,
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com