C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 23rd, 2009, 02:26 AM
yoni162 yoni162 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 19 yoni162 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old June 23rd, 2009, 03:27 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 942 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 6 Days 10 h 36 m 8 sec
Reputation Power: 5
Instead of
plate_ptr==dynamic_cast<...
put
plate_ptr = dynamic_cast<...
__________________
There is no such thing as C/C++, you either program C or C++

Reply With Quote
  #3  
Old June 23rd, 2009, 07:10 AM
yoni162 yoni162 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 19 yoni162 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 1 m 48 sec
Reputation Power: 0
Quote:
Originally Posted by Icon
Instead of
plate_ptr==dynamic_cast<...
put
plate_ptr = dynamic_cast<...


Jeez..stupid mistake..thanks a lot!

Reply With Quote
  #4  
Old June 23rd, 2009, 07:13 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 942 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 6 Days 10 h 36 m 8 sec
Reputation Power: 5
Don't worry, we all make stupid mistakes. It is good that you ask anyway instead of giving up.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Classes - Dynamic_cast failure


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek