dynamic_cast<help>difficulty
I am trying to learn the dynamic_cast, but I am having difficulty running even this simple program.
#include <iostream>
using namespace std;
class A
{
};
class B: public A
{
public:
void SayHi();
};
void B::SayHi()
{
cout<<"Hi"<<endl;
}
int main()
{
B *b = new B;
b->SayHi();
A *a = b;
b = NULL;
b = dynamic_cast<B>(a);
b->SayHi();
delete b;
a = NULL; //Invalid object pointer
return 0;
}
# 1 Re: dynamic_cast<help>difficulty
Take a look at C++ language: What are c-style casts, static_cast, dynamic_cast etc? (http://www.dev-archive.com/forum/showthread.php?t=312456&highlight=cast)
Also, some good discussion you'll find at the followings.
Using dynamic casting to downcast (http://www.dev-archive.com/forum/showthread.php?t=236102)
Dynamic Casting:WHAT am I doing wrong? (http://www.dev-archive.com/forum/showthread.php?t=258097)
performance of dynamic_cast (http://www.dev-archive.com/forum/showthread.php?t=302703)
Ejaz at 2007-11-11 0:28:45 >

# 2 Re: dynamic_cast<help>difficulty
I have read about it, but I can't find the error. I did make a mistake in that b = dynamic_cast<B>(a) should be b = dynamic_cast<B*>(a); however, when I replace the code with that I get error C2683: dynamic_cast : 'A' is not a polymorphic type. How could this not be a polymorphic type, that is why I'm using inheritance?!
# 3 Re: dynamic_cast<help>difficulty
I figured it out. Turns out there must be at least one virtual function in the base class. I do not see why the compiler needs for me to do this but whatever it works.
# 4 Re: dynamic_cast<help>difficulty
How could this not be a polymorphic type, that is why I'm using inheritance?!A class having virtual function table, virtual function table etc. is essentially Polymorphic (runtime polymorphism) type. For this your base class must have atleast one virtual function (pure or non-pure).
By the way, what you want to achieve?
# 5 Re: dynamic_cast<help>difficulty
I figured it out. Turns out there must be at least one virtual function in the base class. I do not see why the compiler needs for me to do this but whatever it works.
Well...as the compiler indicated...'dynamic_cast' is being used for polymorphism. Until there isn't one there is no place for it. In your example, you actually missed that polymorphism. Although you have being inherited there is no virtual function involved in your example. However, even in your example you should have one function virtual by default in order to provide correct inheritence...the destructor of your base class(es). Having a non-virtual destructor in base classes will result in problems and resource leaks while working with polymorphism...you might want to take a look at the following FAQs:
What is polymorphism? (http://www.dev-archive.com/forum/showthread.php?s=&threadid=256008)
How to use virtual functions? (http://www.dev-archive.com/forum/showthread.php?s=&threadid=256624)
# 6 Re: dynamic_cast<help>difficulty
Thanks everyone. Since Ajay Vijay asked, I am trying to write my own windows library, a project I have been working on for about six months. In this library I have a base class called WPWin. Every other class that makes a window inherits it. Here are some of the classes I have come up with.
WPMainWin //used for the main window in an SDK program
WPMDIFrame //used for creating the frame windows in MDI apps
WPMDIChild //used to create an MDI child winodw
The problem occured when I was trying to call a function to get a pointer to an active child window. All the pointers to every windows class are stored in a table of WPWin's, so when I used my hwnd to retrieve a WPWin pointer, I could not access the functions in the WPMDIChild class. This did not allow me to access any of the child window functions and would have become a problem. I could have used virtual functions, but there is no reason for WPWin to have any idea of how a child window works. Now my GetActiveWin function can return a WPMDIChild, a much more useful class, instead of a WPWin. Does this make since.
