Yes you are right, my solution was incorrect.
In this case I would suggest that if you want to ignore
typecasting(which seems the best way to me, if we do not want to change
base class), then using virtual function may solve the issue.
This may require you to add a new function, in the base class. It can be
an empty function, which will add no new functionality to base class. It
will be not a pure virtual function so that you can use Base class to
make its object. Something like following.
#include <iostream>
using namespace std;
class Base
{
public:
void FuncBase()
{
cout<<"I am in Base"<<endl;
}
virtual void FuncA()
{
}
};
class A : public Base
{
public:
void FuncA()
{
cout<<"I am in A"<<endl;
}
};
int main() {
Base *ptr = new A;
ptr->FuncA();
delete ptr;
return 0;
}
From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf
Of abhinav pratap
Sent: Thursday, October 23, 2008 6:13 PM
To: [email protected]
Subject: RE: [c-prog] Type Identification at Runtime for an Object
Hi I tried this method:Base *b;if (conditionA)b = new A;
else if (conditionB)b = new B;else if (conditionC)b = new C;// FunctABC
is in Base, FunctA in Class A, Funct B in Class B and Funct C in class
Cb-> FunctABC; //this is the common function which does not give any
errorb-> FunctA; // This says Class B does not have any
functiona named FunctAb-> FunctB; // This says Class B does not
have any functiona named FunctBb-> FunctC; // This says Class B
does not have any functiona named FunctCAs I suspected this does not
work until i make all the functions in A, B, C common to the Base
Class.Any Idea?????RegardsAbhinav
--- On Thu, 10/23/08, Singh, Vikrant <[EMAIL PROTECTED]
<mailto:Vikrant.Singh%40amd.com> > wrote:
From: Singh, Vikrant <[EMAIL PROTECTED]
<mailto:Vikrant.Singh%40amd.com> >
Subject: RE: [c-prog] Type Identification at Runtime for an Object
To: [email protected] <mailto:c-prog%40yahoogroups.com>
Date: Thursday, October 23, 2008, 5:17 PM
Base *ptr;
if (Condition_For_ A)
ptr = new A;
else if (Condition_For_ B)
ptr = new B;
else if (Condition_For_ C)
ptr = new C;
// Use ptr here, you can call function defined in A,B,C , they need not
to be there in Base class
ptr-> func()
Make Sense?
From: [EMAIL PROTECTED] com [mailto:[EMAIL PROTECTED] com] On Behalf
Of abhinav pratap
Sent: Thursday, October 23, 2008 1:37 PM
To: [EMAIL PROTECTED] com
Subject: [c-prog] Type Identification at Runtime for an Object
Hi,I am facing a serious problem which goes like this:I have three
different classes say A, B and C and i want to create a common object
out of the three classes. It should be such that when I am executing my
program, based on the run time inputs my object will identify itself and
become an object of any of the particular classes A, B or C. The problem
is:1) I don't know of which type should the object be first of all to
compile my program? Void and Type castings don't work.2) There is one
Base Class which is inherited by all these three classes but it does not
contain all the functions of the three classes. And I am reluctant to
modify the BaseClass.3) I want to use the object as a generic one.
Currently it seems that I need to type cast it wherever i am using
it.Thanks in advanceAbhinav
[Non-text portions of this message have been removed]
[Non-text portions of this message have been removed]
[Non-text portions of this message have been removed]
[Non-text portions of this message have been removed]