Problem with dynamic cast

Could someone explain what I'm doing wrong on line 38? I've spent about the last 30 minutes trying to figure out how dynamic_cast works and I just can't figure it out.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>
#include <typeinfo>
#include <algorithm>
#include "Card.cpp"



int main()
{

    std::vector<Card*> Deck;
    Deck.reserve(54);
    //Add 52 standard cards.
    for (int i = Hearts; i <= Spades; i++)
    {
        for (int j = Ace; j <= King; j++)
        {
            Suited card{static_cast<Rank>(j), static_cast<Suit>(i)};
            Deck.push_back(&card);
        }
    }
    //Add two joker cards
    for(int k = Red; k <= Black; k++)
    {
        Unsuited card{static_cast<Color>(k)};
        Deck.push_back(&card);
    }

    std::random_shuffle(Deck.begin(), Deck.end());

    Suited c1;
    Card* cPtr = &c1;
    //Suited& c2 = dynamic_cast<Suited&>(*cPtr);

    for(int i = 0; i < Deck.size(); i++)
    {
        Suited* card = dynamic_cast<Suited*>(Deck[i]);
    }

////Print type(Only prints base class type)
//    for(int i = 0; i < Deck.size(); i++)
//    {
//        std::cout << typeid(Deck[i]).name() << '\n';
//    }
    return 0;
}


Card.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef CARD_H_INCLUDED
#define CARD_H_INCLUDED

enum Rank
{
  Ace,
  Two,
  Three,
  Four,
  Five,
  Six,
  Seven,
  Eight,
  Nine,
  Ten,
  Jack,
  Queen,
  King
};

enum Suit
{
  Hearts,
  Diamonds,
  Clubs,
  Spades
};

enum Color
{
    Red,
    Black
};

class Card
{
    //virtual ~Card(){};
};

class Suited : public Card
{
public:
    Suited() : rank(Ace), suit(Spades)
    {};
    Suited(Rank r, Suit s) : rank(r), suit(s)
    {};
    Rank rank;
    Suit suit;
};

class Unsuited : public Card
{
public:
    Unsuited() : color(Red)
    {};
    Unsuited(Color c) : color(c)
    {};
    Color color;
};

#endif // CARD_H_INCLUDED
Last edited on
> what I'm doing wrong on line 38?

Card is not a polymorphic type. http://en.cppreference.com/w/cpp/language/object#Polymorphic_objects

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <typeinfo>

// A, B, C and D are polymorphic types
struct A { virtual ~A() = default ; };
struct B : virtual A{};
struct C : virtual A{};
struct D : B, C {};

int main()
{
    D d ;
    B* pb = std::addressof(d) ;

    std::cout << dynamic_cast<D*>(pb) << '\n' // down-cast
              << dynamic_cast<C*>(pb) << '\n' ; // cross-cast

    pb = nullptr ;
    std::cout << dynamic_cast<D*>(pb) << '\n' // null
              << dynamic_cast<C*>(pb) << '\n' ; // null

    B b ;
    pb = std::addressof(b) ;
    std::cout << dynamic_cast<D*>(pb) << '\n' // null (cast failed)
              << dynamic_cast<C*>(pb) << '\n' ; // null (cast failed)

    B& rb = d ;
    try
    {
        D& rd = dynamic_cast<D&>(rb) ; // fine: successful down-cast
        std::cout << std::addressof(rd) << '\n' ;

        C& rc = dynamic_cast<C&>(rb) ; // fine: successful cross-cast
        std::cout << std::addressof(rc) << '\n' ;

        B& rb2 = b ;
        D& rd2 = dynamic_cast<D&>(rb2) ; // cast failed, throws std::bad cast
        std::cout << std::addressof(rd2) << '\n' ;
    }
    catch( const std::bad_cast& e ) { std::cout << e.what() << '\n' ; }
}

http://coliru.stacked-crooked.com/a/4afe0ce0223a67c2
Topic archived. No new replies allowed.