Suppose I have defined two classes based on one basic class:
class Basic
{
public:
int i;
};
class DerivedA:public Basic
{
public:
int j;
};
class DerivedB:public Basic
{
public:
int k;
};
Then now have a class named Collect, which contains the pointer to the Basic
class
class Collect
{
public:
Basic *pBasic;
void run();
};
In this class, a function run()
has been defined, which will perform some operations based on the type of the object the pointer points to:
void Collect::run()
{
if (pBasic points to DerivedA object)
{
}
if (pBasic points to DerivedB object)
{
}
};
Then my question is as follows:
- With C++, is it possible to know the type of object the pointer points to?
- Is it a good practice to perform something different based on the type of object the pointer points to as illustrated in
run
function?