473,657 Members | 2,414 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 1574
On 2008-10-08 05:52:43 -0400, Lenin <le***@veveo.ne tsaid:
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...@versatile coding.comwrote :
On 2008-10-08 05:52:43 -0400, Lenin <le...@veveo.ne tsaid:


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
3579
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: -- These two have the same output, other than the width: select dbo.ufn_vbintohexstr(0x123456789abcdef1234) select 0x123456789abcdef1234
3
16571
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, BILLDATE AS , CHKAMT AS , PSDATE AS , POSTDATE AS , TRNSCODE AS , TRLR AS , SUBSTRING(CAST(SVCCODE AS varchar), 1, 4) AS FROM dbo.PAT_Transactions ORDER BY PATNUMBER, SVCCODE
4
10459
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 an unsigned integer, so I see a +1 if the bit is set. Microsoft VC++ thinks it's signed, so I see -1 if the bit is set. Ex. typedef enum {
17
2671
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 cboFont_DrawItem event handler? Why does it need to be cast? -hazz ,................. cboFont.Items.AddRange(FontFamily.Families); } private void cboFont_DrawItem(object sender,
3
21216
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 obj=Activator.CreateInstance(strAssemblyName, "MyClass"); //Now, I want to do something like ((MyClass)obj).Method //Can I do this?
5
3414
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 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
6
6721
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
2713
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: 0x00000000 through 0xFFFFFFFE : Valid byte addresses
5
2358
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 the same alignment requirements. (3) sizeof(double) >= sizeof(unsigned) ===================================== We can cast from double to unsigned int as follows:
7
3945
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
8402
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
8315
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,...
1
8508
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7341
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
5633
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
4164
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2733
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
1627
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.