Board index » cppbuilder » determining original parameter type using typesafe?

determining original parameter type using typesafe?


2005-12-15 11:44:56 PM
cppbuilder46
Greetings.
I am using borland c++ 5.02.
I have this situation:
class classA
{
virtual func1();
};
class classB : public classA
{
};
class classC : public classA
{
};
class classD : public classA
{
};
//declaration
int DFunction(classA & parm1);
//implementation
int DFunction(classA & parm1)
{
if ((TYPESAFE_DOWNCAST(&parm1,classB)) != 0)
{
do stuff for classB only
}
if ((TYPESAFE_DOWNCAST(&parm1,classC)) != 0)
{
do stuff for classC only
}
}
//invocation
int n = DFunction( classB);
n = DFunction(classC);
n = DFunction(classD);
Is this the proper/best/recommended/only way to determine if the
original parameter was either classB, or classC?
It "seems" fine to me, but I wanted to make a brain check.
Thanks
Jeff Kish
 
 

Re:determining original parameter type using typesafe?

Jeff Kish < XXXX@XXXXX.COM >writes:
Quote
class classA
{
virtual func1();
};

class classB : public classA
{
};


class classC : public classA
{
};

class classD : public classA
{
};


//declaration
int DFunction(classA & parm1);
//implementation
int DFunction(classA & parm1)
{
if ((TYPESAFE_DOWNCAST(&parm1,classB)) != 0)
{
do stuff for classB only
}

if ((TYPESAFE_DOWNCAST(&parm1,classC)) != 0)
{
do stuff for classC only
}

}

//invocation
int n = DFunction( classB);

n = DFunction(classC);

n = DFunction(classD);


Is this the proper/best/recommended/only way to determine if the
original parameter was either classB, or classC?
The proper/best/recommended way is not to play such tricks, but to
write a virtual function and call it. The virtual function dispatch
machinery will then cause the right thing to happen.
In the very rare case where a virtual function isn't the way to go,
C++ has the dynamic_cast operator. And before that, Borland had
TYPESAFE_DOWNCAST.
 

Re:determining original parameter type using typesafe?

On Thu, 15 Dec 2005 18:58:20 +0100, XXXX@XXXXX.COM (Thomas Maeder
[TeamB]) wrote:
Quote
Jeff Kish < XXXX@XXXXX.COM >writes:

>class classA
>{
>virtual func1();
>};
>
>class classB : public classA
>{
>};
>
>
>class classC : public classA
>{
>};
>
>class classD : public classA
>{
>};
>
>
>//declaration
>int DFunction(classA & parm1);
>//implementation
>int DFunction(classA & parm1)
>{
>if ((TYPESAFE_DOWNCAST(&parm1,classB)) != 0)
>{
>do stuff for classB only
>}
>
>if ((TYPESAFE_DOWNCAST(&parm1,classC)) != 0)
>{
>do stuff for classC only
>}
>
>}
>
>//invocation
>int n = DFunction( classB);
>
>n = DFunction(classC);
>
>n = DFunction(classD);
>
>
>Is this the proper/best/recommended/only way to determine if the
>original parameter was either classB, or classC?

The proper/best/recommended way is not to play such tricks, but to
write a virtual function and call it. The virtual function dispatch
machinery will then cause the right thing to happen.


In the very rare case where a virtual function isn't the way to go,
C++ has the dynamic_cast operator. And before that, Borland had
TYPESAFE_DOWNCAST.
So plaster all the code the classes need to share into a virtual
function and let the various derived classes hash it out.
Well, OK.
The way the app is constructed, all the work is done outside the
classes. So originally similar functions where overloaded for
classD,classC,classB.
But I wanted to get rid of all those virtually identical functions by
using the base class.
I noticed in some cases the overloaded functions had minor differences
which I was going to address inside the new function by using the
TYPESAFE_DOWNCAST macro.
Are you saying the dynamic_cast operator is the way to go here?
Thanks
Jeff Kish
 

{smallsort}

Re:determining original parameter type using typesafe?

Jeff Kish < XXXX@XXXXX.COM >writes:
Quote
Are you saying the dynamic_cast operator is the way to go here?
dynamic_cast is Standard C++, while TYPESAFE_DOWNCAST was a Borland
only extension.
So if your implementation supports dynamic_cast, I'd go with that. If
not, I'd go with anything that works - and if that's a non-Standard
extension, I'd hide its usage behind an interface so that using it
won't hurt too much when you migrate to some other implementation.