S
Simon
Hi,
If I have two classes.
Class A
{
public:
A( const char *sz);
~A();
virtual void Foo() = 0;
}
Class B : public A
{
public:
B(const char *sz) : A( sz ) {};
~B(){ /* do clean up */ };
virtual void Foo();
int iSomeObject;
char *pSomePointer;
}
if i do
A *test = new A();
//
// is it then safe to do
//
((B *)test)->Foo();
// or even
((B *)test)->iSomeObject = 10
//////////////////////////////////////////////
Why do I ask?
because my base class reads information from a file.
So it is easier for me to have one call
A * test = new A( /* file name to read */ )
Depending on the actual data in the file a certain base class will be
used...
I could do
B * test = new B( /* file name to read */ )
but as I said, I only know what derived class to use after the file has been
read.
I guess I could do it the long way around, (read the file and depending on
the type call the relevant derived class), but that does not seem very
efficient.
To make matters worse I want to save all the pointers in a vector.
std::vector< A* >
but is it safe to include derived classes pointer in a vector?
Many thanks in advance for your help/advices.
Simon
If I have two classes.
Class A
{
public:
A( const char *sz);
~A();
virtual void Foo() = 0;
}
Class B : public A
{
public:
B(const char *sz) : A( sz ) {};
~B(){ /* do clean up */ };
virtual void Foo();
int iSomeObject;
char *pSomePointer;
}
if i do
A *test = new A();
//
// is it then safe to do
//
((B *)test)->Foo();
// or even
((B *)test)->iSomeObject = 10
//////////////////////////////////////////////
Why do I ask?
because my base class reads information from a file.
So it is easier for me to have one call
A * test = new A( /* file name to read */ )
Depending on the actual data in the file a certain base class will be
used...
I could do
B * test = new B( /* file name to read */ )
but as I said, I only know what derived class to use after the file has been
read.
I guess I could do it the long way around, (read the file and depending on
the type call the relevant derived class), but that does not seem very
efficient.
To make matters worse I want to save all the pointers in a vector.
std::vector< A* >
but is it safe to include derived classes pointer in a vector?
Many thanks in advance for your help/advices.
Simon