| | |
error dynamic_cast in c++
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 1
Reputation:
Solved Threads: 0
I meets some problem with downcast in c++ programming language.
Thus, I needs your help.
My program has 5 classes. They are Employee (abstract class),Salary:public Employee,Hourly:public Employee,Commission:public Employee and BasePlusCom:public Commission.
In main() function,I used vector<Employee*>employees[4];
Then I assigned values to employees.
........
And I used structural syntax BasePlusCom* ComPtr=dynamic_cast<BasePlusCom*>(employees[i]);
.....
And when compiles this program, compiler produces a warning that:
"dynamic_cast" used on polymorphic type 'class Employee' with G/R; unpredictable behavior may result.
And when I change dynamic_cast to reinterpret_cast then program run, but it not work properly.
How does casting in C++ differ?
Thus, I needs your help.
My program has 5 classes. They are Employee (abstract class),Salary:public Employee,Hourly:public Employee,Commission:public Employee and BasePlusCom:public Commission.
In main() function,I used vector<Employee*>employees[4];
Then I assigned values to employees.
........
And I used structural syntax BasePlusCom* ComPtr=dynamic_cast<BasePlusCom*>(employees[i]);
.....
And when compiles this program, compiler produces a warning that:
"dynamic_cast" used on polymorphic type 'class Employee' with G/R; unpredictable behavior may result.
And when I change dynamic_cast to reinterpret_cast then program run, but it not work properly.
How does casting in C++ differ?
dynamic_cast can be used only for polymorphic classes.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
you need to enable RTTI. (use compiler switches for this.)
either you have turned it off by using the switch /GR-
or you are using a fairly old version of the microsoft compiler.
turn it on with the compiler switch /GR
http://msdn.microsoft.com/en-us/library/we6hfdy0.aspx
either you have turned it off by using the switch /GR-
or you are using a fairly old version of the microsoft compiler.
turn it on with the compiler switch /GR
http://msdn.microsoft.com/en-us/library/we6hfdy0.aspx
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: error: expected constructor, destructor, or type conversion before '<' to
- Next Thread: Structure help
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






