[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Lenin

10/8/2008 9:53:00 AM

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.
2 Answers

Pete Becker

10/8/2008 10:16:00 AM

0

On 2008-10-08 05:52:43 -0400, Lenin <lenin@veveo.net> said:

> 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)

Lenin

10/8/2008 11:35:00 AM

0

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.com> wrote:
> On 2008-10-08 05:52:43 -0400, Lenin <le...@veveo.net> said:
>
>
>
>
>
> > 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)