473,225 Members | 308 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,225 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 1522

"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: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.