S
SunScreen
Hi all,
I would like to help with the following:
// my conventions:
// BC = Base Class
// DC = Derived Class
class BC // this ADT base class or interface
{
BC(){}
~BC(){}
bcFunc1() = 0;
bcFunc2() = 0;
}
class DC : public BC
{
DC(){}
~DC(){}
bcFunc1() { // Do something }
bcFunc2() { // Do something }
dcFunc() { // Do something }
}
int main()
{
// I declare a pointer to BC assigned to DC object
BC* myBC = new DC;
myBC->bcFunc1();
myBC->bcFunc2();
// now the tricky part - the following line will not compile
// myBC->dcFunc();
// one way to handle this is using the dynamic_cast operator
DC* myDC = dynamic_cast<DC*> (myBC);
// now I can call the DC function
myDC->dcFunc();
delete myBC;
delete myDC;
}
I know that using the dynamic_cast operator is bad programming code.
Is there any other way call the DC function?
Thanx in Advance,
Sun
I would like to help with the following:
// my conventions:
// BC = Base Class
// DC = Derived Class
class BC // this ADT base class or interface
{
BC(){}
~BC(){}
bcFunc1() = 0;
bcFunc2() = 0;
}
class DC : public BC
{
DC(){}
~DC(){}
bcFunc1() { // Do something }
bcFunc2() { // Do something }
dcFunc() { // Do something }
}
int main()
{
// I declare a pointer to BC assigned to DC object
BC* myBC = new DC;
myBC->bcFunc1();
myBC->bcFunc2();
// now the tricky part - the following line will not compile
// myBC->dcFunc();
// one way to handle this is using the dynamic_cast operator
DC* myDC = dynamic_cast<DC*> (myBC);
// now I can call the DC function
myDC->dcFunc();
delete myBC;
delete myDC;
}
I know that using the dynamic_cast operator is bad programming code.
Is there any other way call the DC function?
Thanx in Advance,
Sun