943,694 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1250
  • C++ RSS
Nov 16th, 2008
0

error dynamic_cast in c++

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gammavn is offline Offline
1 posts
since Nov 2008
Nov 17th, 2008
0

Re: error dynamic_cast in c++

dynamic_cast can be used only for polymorphic classes.
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Nov 17th, 2008
0

Re: error dynamic_cast in c++

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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Nov 17th, 2008
0

Re: error dynamic_cast in c++

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
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Problem with OOP and classes...
Next Thread in C++ Forum Timeline: Structure help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC