summaryrefslogtreecommitdiff
path: root/graphs/cpp/smtptr/shared_pointer_is_a.cc
blob: b6dcb95267fcc68aaf6a0918fe51ce9424667d44 (plain)
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
#include "shared_pointer.hh"

class Animal
{
public:
    virtual ~Animal()
    {}
};

class Cat : public Animal
{
public:
    void meow()
    {
        std::cout << "Meow!\n";
    }
};

class Dog : public Animal
{
public:
    void bark()
    {
        std::cout << "Woof!\n";
    }
};

int main()
{
    SharedPointer<Animal> animalPtr{ new Cat };
    if (animalPtr.is_a<Cat>())
    { // true
        std::cout << "The pointer points to a Cat.\n";
    }

    SharedPointer<Animal> animalPtr2{ new Dog };
    if (animalPtr2.is_a<Cat>())
    { // false
        std::cout << "The pointer points to a Cat.\n";
    }
    else if (animalPtr2.is_a<Dog>())
    {
        std::cout << "The pointer points to a Dog.\n";
    }

    return 0;
}