Oct 18, 2009 at 1:24pm UTC
what is wrong with these codes and why?
#include "iostream"
using namespace std;
class base
{
public:
virtual ~base(){}
void work(){_do_work();}
private:
virtual void _do_work();
};
class child:public base
{
child();
private:
void _do_work(){}
};
void main()
{
child test;
test.work();
base *pb=&test;
pb->work();
child *pc = dynamic_cast<child *>pb;
pc->work();
delete pb;
return;
}
Oct 18, 2009 at 2:39pm UTC
Last edited on Oct 18, 2009 at 2:44pm UTC
Oct 19, 2009 at 3:11am UTC
I've correct the errors above, but find that
"child *pc = dynamic_cast<child *>(pb);"
will be run-error, what is the reason?
Oct 19, 2009 at 12:26pm UTC
no idea. can you post your full current code?
also, there is another problem with your code. counstructors should be public. (child::child() is private, becouse by default classes have private access type)
Last edited on Oct 19, 2009 at 12:26pm UTC
Oct 19, 2009 at 4:48pm UTC
delete pb
is wrong as the object was declared on the stack with child test;
There's nothing wrong with the rest of it.