error dynamic_cast in c++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 1
Reputation: gammavn is an unknown quantity at this point 
Solved Threads: 0
gammavn gammavn is offline Offline
Newbie Poster

error dynamic_cast in c++

 
0
  #1
Nov 16th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,821
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 117
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: error dynamic_cast in c++

 
0
  #2
Nov 17th, 2008
dynamic_cast can be used only for polymorphic classes.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: error dynamic_cast in c++

 
0
  #3
Nov 17th, 2008
Post the code please. I can see a few ways around this but it might be better to have a sense of the intent of the program before making a suggestion.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: error dynamic_cast in c++

 
0
  #4
Nov 17th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC