"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message news:<40************@jpl.nasa.gov>...
C++fan wrote:
Below is simple code:
class bs {
public:
virtual void handle(void) const = 0;
};
class driv: public bs {
public:
/* virtual */ void handle(void) const;
};
void driv::handle(void) const {
std::cout << "Hello!" << std::endl;
}
void funct(const bs* b){
b->handle();
}
In funct(const bs*), will b->handle() invoke driv::handle(void)?
Or bs::handle(void)?
You can't instantiate an object of type bs
so b->handle() must invoke member function handle(void)
defined by some class derived from bs.
The only class that you have derived from bs so far is driv
so, if you write:
const driv d;
funct(&d);
your program will write
Hello!
to standard output.
Thanks. I have tested it. It is the same result.
Now I add two more derived classes as below:
class driv1: public bs {
public:
/* virtual */ void handle(void);
};
void driv1::handle(void) {
std::cout << "Hello! Hello!" << std::endl;
}
class driv2: public driv {
public:
/* virtual */ void handle(void);
};
void driv2::handle(void){
std::cout << "Hello! Hello! Hello!" << std::endl;
}
and
driv* a1 = new driv;
funct(a1);
driv1* a2 = new driv1;
funct(a2);
driv2* a3 = new driv2;
funct(a3);
What will be the respective result?
Thanks.
Jack