470,591 Members | 1,579 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,591 developers. It's quick & easy.

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 1433

"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Picture Dots | last post: by
4 posts views Thread by Hal Vaughan | last post: by
7 posts views Thread by crypto_stonelock | last post: by
7 posts views Thread by ralphNOSPAM | last post: by
2 posts views Thread by FR3AK | last post: by
11 posts views Thread by axlq | last post: by
3 posts views Thread by Martin Racette | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.