473,503 Members | 2,135 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cast

Hi,
I have a multiply inherited class "C" as folloows. None of
them are inherited as virtual base class except that they have virtual
functions.

class b1
{
....
virtual void f1() {}
}

class b2
{
....
virtual void f2() {}
}

class c: pulbic b1, public b2
{
.....
}

Now that, I have a function fun() which takes b1's pointer and now
I want to call b2's functions from there. Assume that the function
is called with argument as address of c's object like below.

void fun(b1 *bp1)
{
b2 *bp2 = (b2*) (c*) bp1;
bp2->f2();
}
somewhere I call like this

c obj;
fun(&obj);

This works fine. But in this case, do I need to use dynamic cast
inside the function fun? Assume that the runtime error check for junk
pointer is not needed in my case.

Thanks in advance.
Oct 8 '08 #1
2 1568
On 2008-10-08 05:52:43 -0400, Lenin <le***@veveo.netsaid:
Hi,
I have a multiply inherited class "C" as folloows. None of
them are inherited as virtual base class except that they have virtual
functions.

class b1
{
...
virtual void f1() {}
}

class b2
{
...
virtual void f2() {}
}

class c: pulbic b1, public b2
{
....
}

Now that, I have a function fun() which takes b1's pointer and now
I want to call b2's functions from there.
Seems like the function ought to take a c*. Then you won't have to mess
around with conversions.
Assume that the function
is called with argument as address of c's object like below.

void fun(b1 *bp1)
{
b2 *bp2 = (b2*) (c*) bp1;
bp2->f2();
}
somewhere I call like this

c obj;
fun(&obj);

This works fine. But in this case, do I need to use dynamic cast
inside the function fun? Assume that the runtime error check for junk
pointer is not needed in my case.
If you'll always be able to convert a b1* to a b2*, then you can use
the C style casts as written above, replace them with static_casts, or
write the function so that it's interface tells the truth. If it only
deals with c*'s, then it should take a c* as its argument.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 8 '08 #2
Lets say the function fun and the b1,b2 class are part of some library
and fun is a callback. Then in that case, will the c style casting
works irrespective of the compiler/platform?

On Oct 8, 3:16*pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-10-08 05:52:43 -0400, Lenin <le...@veveo.netsaid:


Hi,
* * * *I have a multiply inherited class "C" as folloows. * None of
them are inherited as virtual base class except that they have virtual
functions.
class b1
{
...
virtual void f1() {}
}
class b2
{
...
virtual void f2() {}
}
class c: pulbic b1, public b2
{
....
}
* * Now that, I have a function fun() which takes b1's pointer and now
I want to call b2's functions from there.

Seems like the function ought to take a c*. Then you won't have to mess
around with conversions.


* *Assume * that the function
is called with argument as address of c's object like below.
void fun(b1 *bp1)
{
* *b2 *bp2 = (b2*) (c*) bp1;
* *bp2->f2();
}
somewhere I call like this
c obj;
fun(&obj);
This works fine. But in this case, do I need to use dynamic cast
inside the function fun? *Assume that the runtime error check for junk
pointer is not needed in my case.

If you'll always be able to convert a b1* to a b2*, then you can use
the C style casts as written above, replace them with static_casts, or
write the function so that it's interface tells the truth. If it only
deals with c*'s, then it should take a c* as its argument.

--
* Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
Oct 8 '08 #3

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

Similar topics

0
3573
by: Aaron W. West | last post by:
Fun with CAST! (Optimized SQLServerCentral script posts) I found some interesting "tricks" to convert binary to hexadecimal and back, which allow doing 4 or 8 at a time. Test code first: --...
3
16561
by: Mike | last post by:
I am using MS-Access as a front end for my MS-SQL DB. I have a sql view that uses the following: SELECT TOP 100 PERCENT RECID, PATNUMBER AS , SVCCODE AS , QTY, PROF_CHRGS AS , AMOUNT,...
4
10422
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is...
17
2660
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
3
21199
by: mra | last post by:
I want to cast an object that I have created from a typename to the corresponding type. Can anycone tell me how to do this? Example: //Here, Create the object of type "MyClass" object...
5
3396
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
6
6710
by: Lore Leunoeg | last post by:
Hello I derived a class MyControl from the Control class. Public Class MyControl Inherits Control Sub New() MyBase.New() End Sub End Class
9
2697
by: Frederick Gotham | last post by:
Let's assume that we're working on the following system: CHAR_BIT == 8 sizeof( char* ) == 4 (i.e. 32-Bit) Furthermore, lets assume that the memory addresses are distributed as follows: ...
5
2344
by: Frederick Gotham | last post by:
Before I begin, here's a list of assumptions for this particular example: (1) unsigned int has no padding bits, and therefore no invalid bit- patterns or trap representations. (2) All types have...
7
3937
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
0
7205
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
7093
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
7287
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,...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
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
5596
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,...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
747
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.