Hey guys I am writing code that uses a dynamic casts then prints out some objects. What I am having trouble with is my code test as null when it isn't suppose to. Here is what I got and the dynamic cast code looks dead on to me. Maybe not though.

Circle* c=dynamic_cast<Circle*>(*itr);
        if(c != NULL)
                c->print();
        else
                cout << "error" << endl;

It looks OK, but that is with a few assumptions. Can you post a complete example that fails when it should not?

This is my driver program where this problem is located. Could it have to do with my iterator?

#include "Shape.h"
#include "Circle.h"
#include "Rectangle.h"
#include "Triangle.h"
#include <list>
#include <iostream>

using namespace std;

int main()
        {
        list<Shape *> L;
        list<Shape *>::iterator itr;

        L.push_front(new Circle("blue", 11));
        L.push_front(new Circle("green" , 5));

        L.push_front(new Rectangle("purple", 5, 15));
        L.push_front(new Rectangle("red", 15, 27));

        L.push_front(new Triangle("yellow", 10, 3));
        L.push_front(new Triangle("black", 3, 30));

        cout << "\nPrinting all shapes..." << endl << endl;

        for(itr = L.begin(); itr != L.end(); ++itr)
                (*itr)->print();

        cout << "\nPrinting only circles..." << endl << endl;

        Circle* c=dynamic_cast<Circle*>(*itr);
        if(c != NULL)
                c->print();
        else
                cout << "error" << endl;

        return 0;
        }

This is my driver program where this problem is located. Could it have to do with my iterator?

#include "Shape.h"
#include "Circle.h"
#include "Rectangle.h"
#include "Triangle.h"
#include <list>
#include <iostream>

using namespace std;

int main()
        {
        list<Shape *> L;
        list<Shape *>::iterator itr;

        L.push_front(new Circle("blue", 11));
        L.push_front(new Circle("green" , 5));

        L.push_front(new Rectangle("purple", 5, 15));
        L.push_front(new Rectangle("red", 15, 27));

        L.push_front(new Triangle("yellow", 10, 3));
        L.push_front(new Triangle("black", 3, 30));

        cout << "\nPrinting all shapes..." << endl << endl;

        for(itr = L.begin(); itr != L.end(); ++itr)
                (*itr)->print();

        cout << "\nPrinting only circles..." << endl << endl;

        Circle* c=dynamic_cast<Circle*>(*itr);
        if(c != NULL)
                c->print();
        else
                cout << "error" << endl;

        return 0;
        }

Your dynamic_cast is fine, but take another look at your code..You've iterated through the list and printed all items, then you've printed a message saying that you're going to print only the circles.
Next is where you're going wrong. You're trying to dynamic_cast the iterator....now at this point the iterator will be pointing to the last element in the list, if the last element is a circle then the dynamic_cast will work.

But what you really need to be doing is iterating through the list again and attempting to dynamic_cast all of the items in the list like this:

cout << "\nPrinting only circles..." << endl << endl;

        // iterate through the list and try casting all elements to circle..
        for(itr = L.begin(); itr != L.end(); ++itr)
        {
            Circle* c=dynamic_cast<Circle*>(*itr);
            if(c != NULL)
                    c->print();
            else
                    cout << "error" << endl;
        }

Cheers for now,
Jas.

Edited 7 Years Ago by JasonHippy: n/a

This article has been dead for over six months. Start a new discussion instead.