473,791 Members | 3,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

finding offset of subclasses

Dear all,

Is there any way to derive the offset of a subclass without instantiating
the class (in other words none of the *_cast operators can be used).

So, e.g.:

class A {};
class B {};
class C : public A, public B {};

I want to know the offset of B within C.

Thanks,
Petar
Jul 22 '05 #1
5 2554
On 9 Feb 2004 17:50:48 -0800, pe***@scs.cs.ny u.edu (Petar#M) wrote in
comp.lang.c++:
Dear all,

Is there any way to derive the offset of a subclass without instantiating
the class (in other words none of the *_cast operators can be used).

So, e.g.:

class A {};
class B {};
class C : public A, public B {};

I want to know the offset of B within C.

Thanks,
Petar


Why do you want to know this? Perhaps you need to rethink your
design.

In any case, if the class is a POD class, which means respectively
that all its base classes are POD classes, the standard offsetof()
macro from <stddef.h> or <cstddef> can be used.

If it's not a POD class, there is no standard C++ way to do this.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #2
Jack Klein wrote:

If it's not a POD class, there is no standard C++ way to do this.


And there may not be a single value: virtual bases can be at different
offsets in the same derived type, depending on the most derived type.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #3
"Pete Becker" <pe********@acm .org> wrote...
Jack Klein wrote:

If it's not a POD class, there is no standard C++ way to do this.


And there may not be a single value: virtual bases can be at different
offsets in the same derived type, depending on the most derived type.


I am not sure I understand that. So, if 'B' is a stand-alone object,
it can have a different layout than if it's a sub-object of another
object, when virtual inheritance is involved?

Thanks.

Victor
Jul 22 '05 #4
Victor Bazarov wrote in news:f26Wb.2655 95$I06.2850867@ attbi_s01:
"Pete Becker" <pe********@acm .org> wrote...
Jack Klein wrote:
>
> If it's not a POD class, there is no standard C++ way to do this.
>


And there may not be a single value: virtual bases can be at different
offsets in the same derived type, depending on the most derived type.


I am not sure I understand that. So, if 'B' is a stand-alone object,
it can have a different layout than if it's a sub-object of another
object, when virtual inheritance is involved?


yes,

struct B_base { int bb; };

struct B : virtual B_base
{
int data;
};

actual layout of b is say:

struct __like_B
{
__implementatio n_defined __ptr_to_B_base ;
int data;
B_base __actual_B_base ; /* actual subobject */
};

struct C: B_base, B
{
};

actual layout of C:

struct __like_C
{
B_base __actual_B_base ; /* C & B subobject */
__implementatio n_defined __ptr_to_B_base ; /* B */
int data; /* B */
};

__ptr_to_B_base need to be initialized by B or C's ctor.

int get_bb( B *bp )
{
return bp->bb;

gets translated to something like:

return bp->__ptr_to_B_bas e->bb;
}

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #5
Victor Bazarov wrote:

"Pete Becker" <pe********@acm .org> wrote...
Jack Klein wrote:

If it's not a POD class, there is no standard C++ way to do this.


And there may not be a single value: virtual bases can be at different
offsets in the same derived type, depending on the most derived type.


I am not sure I understand that. So, if 'B' is a stand-alone object,
it can have a different layout than if it's a sub-object of another
object, when virtual inheritance is involved?


Yup. Let's look at two classes, B and C, each with a virtual base A:

+--------+--------+
| B data | A data |
+--------+--------+

+--------+--------+
| C data | A data |
+--------+--------+

Now derived a class D from both B and C. One possible layout is:

+--------+--------+--------+--------+
| D data | B data | A data | C data |
+--------+--------+--------+--------+

Another is:

+--------+--------+--------+--------+
| D data | B data | C data | A data |
+--------+--------+--------+--------+

In the first, the offset of the A subobject from the C subobject is
different from its offset in a standalone C object. In the second it's
the offset from the B subobject that's different.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #6

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

Similar topics

4
1939
by: Klaus Neuner | last post by:
Hello, the function given below returns all indexes of list2 where a sublist of list2 that is identical to list1 begins. As I will need this function quite often, I would like to know if more experienced programmers would agree with the way I defined the function: - Is there a more efficient way to do it? (Apart from building
5
2900
by: Thomas Philips | last post by:
I'm teaching myself programming using Python, and have a question about subclasses. My game has two classes, Player and Alien, with identical functions, and I want to make Player a base class and Alien a derived class. The two classes are described below class Player(object): #Class attributes for class Player threshold = 50 n=0 #n is the number of players
13
15267
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the pattern can occur on any byte boundary in the file, so chunking through the code at 16 bytes a frame maybe a problem. The file itself isn't so large, maybe 32 kbytes is all and the need for speed is not so great, but the need for accuracy in the...
0
1308
by: Vera | last post by:
Hi, I have a very annoying problem, with which I NEED HELP DESPERATELY!! It smells like a bug to me, but I'm not sure. SITUATION This description is a very much simplified version of the real situation. I have the following class structure: ASSEMBLY ATools
8
3006
by: Marco | last post by:
Hi all, I have a base class and some subclasses; I need to define an array of objects from these various subclasses. What I have is something like: { //I have a base class, something like: class CPeople {
2
4420
by: MR | last post by:
I'm hoping that someone here will be able to direct me to some litrature on how to get a list of a classes subclasses. I.e. need the inherited class to know about subclasses that are inherited form it. I guess that I'll have to use reflection but I can't find any examples on MSDN. Thanks in advance, Mark
19
8770
by: Rahul | last post by:
Hi, Is there a way to find the offset of a class member at compile time. e.g. class A{ int i; int j; char c; }; Here the offset of c = 8 bytes from the start of an object of A (assuming 4 byte int). Can it be done at compile time. Thanks in advance
17
3790
by: abhimanyu.v | last post by:
Hi Guys, I have one doubt. The test program is given below. It uses two way of finding out the offset of a variable in structure. I executed the program and found the same result. My question is what is difference between 1) (unsigned long) &((struct foobar *)0)->foo and
15
1975
by: rhino | last post by:
I've put together a prototype of two-tiered CSS tabs that works really well in IE6, IE7, and FF2. It also works very well in Opera 9.27 _except_ that the placement of the lower tier of tabs is messed up. Both the XHTML and CSS validate without any errors or warnings. Can anyone help me figure out what is wrong? I'd be especially interested in the technique you used to figure out where the problem was. I still struggle a lot when I try to...
0
9669
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9517
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10428
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9997
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.