
June 23rd, 2009, 02:26 AM
|
|
Registered User
|
|
Join Date: Jun 2009
Posts: 19
Time spent in forums: 5 h 1 m 48 sec
Reputation Power: 0
|
|
|
Classes - Dynamic_cast failure
Ok, first here is the relevant code:
Code:
bool Plate::isSame(Dish* dishptr) const
{
std::cout<<"in function Plate::isSame"<<std::endl;
Plate* plate_ptr=0;
plate_ptr==dynamic_cast<Plate*>(dishptr); //casting fails
if(plate_ptr==NULL) {std::cout<<"dynamic cast failed"<<std::endl;}
if(plate_ptr!=NULL) //if 'dishptr' actually points to a 'Plate' object
{
std::cout<<"dynamic cast successful"<<std::endl;
if(Dish::isSame(plate_ptr) && RoundDish::isSame(plate_ptr)) //these functions expect fathers and get sons
{
if(withPic==plate_ptr->withPic)
{
std::cout<<"Plate::isSame is match"<<std::endl;
return 1;
}
}
}
else
{
std::cout<<"Plate::isSame no match"<<std::endl;
return 0;
}
return 0;
}
In short, this function receives a base class pointer (Dish*) and tries to see if it actually points to a derived class object (Plate), and if so, compares between the two of them. I've isolated the problem to the line:
plate_ptr==dynamic_cast<Plate*>(dishptr); //casting fails
since I get the line "dynamic cast failed" right after it executes..I can't figure out why the casting fails, even when manually calling the function with a matching derived-type object. I've included the call for the function (done from within another class):
Code:
void DishSortingMachine::Sort( Dish* dish )
{
std::cout<<"in function SORT"<<std::endl;
int match=0, added=0;
for(std::vector<const Dish*>::size_type j=0;j<m_vctCorrectDishes.size();j++)
{
if(m_vctCorrectDishes[j]->isSame(dish))
This isn't the entire function, just the relevant part. Thanks for any help.
|