473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Detecting classes with virtual functions

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! ]
Jul 22 '05 #1
5 1708
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


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #2
"News Admin" <ne**@news.demon.net> wrote in message news:<ce*******************@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.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #3
"News Admin" <ne**@news.demon.net> wrote in message news:<ce*******************@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)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #4
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?
Jul 22 '05 #5
"News Admin" <ne**@news.demon.net> wrote in message news:<ce*******************@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

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Querejeto | last post by:
Hello: Is it possible to detect programmatically the constness of a member function when it is called? That is, I would like to see a generic implementation (i.e. it does not depend on the...
5
by: Bilgehan.Balban | last post by:
Hi, I am currently brushing up my c++ knowledge and I would like to ask you about the differences between classes and C structs, in the function/method perspective. 1) Is it correct to say...
10
by: Bit byte | last post by:
I have a set of C++ classes for which I want to provide a C API - and compile into a C DLL, for use in an application that can only work with a C interface DLL. Any suggestions/pointers on how...
7
by: Amu | last post by:
Hi i am stuck up in a part of project where i have to inherit the VC++ template classes into C#.net. Please provide me the solution for this .
2
by: Amu | last post by:
i have a dll ( template class) ready which is written in VC++6. But presently i need to inherit its classes into my new C#.net project.so if there is some better solution with u then please give me...
4
by: KishorAditya | last post by:
Hi All, Consider the following scenario: class Top { }; class Left: virtual public Top { }; class Right: virtual public Top { }; class Bottom: public Left, public Right {}; Many books propose...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
13
by: Ilias Lazaridis | last post by:
How to detect memory leaks of python programms, which run in an environment like this: * Suse Linux 9.3 * Apache * mod_python The problem occoured after some updates on the infrastructure....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.