473,320 Members | 1,926 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,320 software developers and data experts.

finding the class address

Lets say i have class A class B and class C.

Class C inherites class B and class B inherites class A. Class A contains a
vertual function (not pure) and class C contains the same vertual function.

In my code i get the address of class A (i can't get the address of the
other classes, its children) and then call the vertual function. It then in
turn calls the vertual function of class C. Is there any way of getting the
address of class C, c++ seems to have done it some how cos it called the
function in class C instaed of class A.

Thx for any help

Scott
Jul 22 '05 #1
2 1526

"scott" <sc***********@hotmail.com> wrote in message
news:ck**********@newsg2.svr.pol.co.uk...
Lets say i have class A class B and class C.

Class C inherites class B and class B inherites class A. Class A contains
a
vertual function (not pure) and class C contains the same vertual
function.

In my code i get the address of class A (i can't get the address of the
other classes, its children) and then call the vertual function. It then
in
turn calls the vertual function of class C. Is there any way of getting
the
address of class C, c++ seems to have done it some how cos it called the
function in class C instaed of class A.
Well not exactly. The way virtual functions are usually implemented is that
each object carries around a pointer to a table of function pointers. Your C
object is carrying a pointer to a table of C function pointers. So when you
call a virtual function using an A pointer it still calls the C function
because it looks up, at run time, the function to call in the table.

Thx for any help

Scott


What you are asking to do is called a downcast. You want to go from A* to
C*, which is down the inheritance hierarchy.

Obviously this is dangerous because you cannot be sure that your A* variable
is really pointing to a C object. But if you are really, really sure then
you can simply to a static_cast

A* ptr = ...;
C* ptr2 = static_cast<C*>(ptr);

If you are wrong this will most likely crash your program.

If you aren't really sure then you can do a dynamic_cast

A* ptr = ...;
C* ptr2 = dynamic_cast<C*>(ptr);
if (ptr2 != NULL)
{
...

The difference is that if ptr wasn't pointing to a C object then dynamic
cast will return NULL.

I don't know why you want to do this, but normally it is better to add more
virtual functions to your classes than to try downcasting. Often when you
think you need to downcast what it really means is that you need to think a
harder about how your classes are designed.

john

Jul 22 '05 #2
scott wrote:
Lets say i have class A class B and class C.

Class C inherites class B and class B inherites class A. Class A contains a vertual function (not pure) and class C contains the same vertual function.
In my code i get the address of class A (i can't get the address of the
other classes, its children) and then call the vertual function. It then in turn calls the vertual function of class C. Is there any way of getting the address of class C, c++ seems to have done it some how cos it called the
function in class C instaed of class A.


The short answer is there must be a better way to do what you are trying,
and the C and A objects are the same and at the same address.

The long answer is compiler-specific, so you will get better results if you
ask a better question on a newsgroup dedicated to your compiler. Such a
question should include the "outer problem" that you try to solve.

Most C++ classes work like this:

class A
{
vtable * vptr; // <-- invisible member
int whatever;
virtual void funk();
};

A::vtable[] = {
&A::funk,
};

Your debugger might be able to show you that secret pointer. I made up the
syntax for its table because the real thing would be too complex.

Here's your C class:

class C: public A
{
...
virtual void funk();
};

C::vtable[] = {
&C::funk,
};

When C constructs, its constructor evaluates A::vptr = C::vtable. That's how
the inheritance happens. All calls to funk() are really calling vptr[index],
where 'index' is the hardcoded offset of each virtual thing in A or its
descendents.

So calls to funk, even thru references to A, will route to C because it
replaced the vtable, and because &C::funk occupies the same index in the
table.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #3

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

Similar topics

11
by: Picture Dots | last post by:
Ok - I want to check a string for @ in it (Im checking to see if a string that should contain an email holds a @ ) How would I go about this? preg_match? ereg? strstr?Examples? Also - how...
4
by: Hal Vaughan | last post by:
I want to have a config file for my program, which means I need to know where the config file is. If I type: java myclass and it runs myclass.class, is there any way to obtain the location of...
7
by: crypto_stonelock | last post by:
Hello, I was wondering if anybody knew how to retrieve the IP address attributed dynamically by your ISP without having to use the info collected on a socket with say .getLocalAddress. I'm...
7
by: ralphNOSPAM | last post by:
Is there a PHP script that can find unused variables? I'd like to 'clean up' my scripts. Thanks...
2
by: FR3AK | last post by:
Hi. does anyone have any idea of how to find the mac address of a remote networking device? I've worked with WMI and it works fine as long as the target machine is a Windows computer, however,...
11
by: axlq | last post by:
Does anyone have a favored way of finding the geographic location of a user's IP address, so that a php script can include content relevant to that location? Reverse-lookup of the hostname isn't...
3
by: Martin Racette | last post by:
Hi, I'm making am app that check which IP is currently use on my network, but I would like to be able to find out the name of the computer to whom the IP belong, and I can not find out how to do...
17
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...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.