Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Detecting classes with virtual functions

3 views
Skip to first unread message

News Admin

unread,
Jul 27, 2004, 12:25:17 PM7/27/04
to
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.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Marco Manfredini

unread,
Jul 27, 2004, 10:34:57 PM7/27/04
to
News Admin wrote:

> 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

David Baraff

unread,
Jul 27, 2004, 11:09:29 PM7/27/04
to
"News Admin" <ne...@news.demon.net> wrote in message news:<ce5hc1$npe$1$830f...@news.demon.co.uk>...

> 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.

John Torjo

unread,
Aug 2, 2004, 10:25:40 AM8/2/04
to
"News Admin" <ne...@news.demon.net> wrote in message news:<ce5hc1$npe$1$830f...@news.demon.co.uk>...
> 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
-- 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)

red floyd

unread,
Aug 2, 2004, 10:51:44 AM8/2/04
to
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?

David Abrahams

unread,
Aug 4, 2004, 6:16:30 AM8/4/04
to
"News Admin" <ne...@news.demon.net> wrote in message news:<ce5hc1$npe$1$830f...@news.demon.co.uk>...
> 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,
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

0 new messages