is it possible ?

lets say we have a class animal //which is base
and we have a class dog,cat,horse that inherits the class animal
can i make a pointer from the type animal and then it would go to a function of one of the other classes as i choose ? im trying to make them with one pointer!
Try this example

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
#include <iostream>
#include <initializer_list>
 
struct Animal
{
   virtual void what() const = 0;
   virtual ~Animal() {}
};
 
struct Dog : Animal
{
   void what() const { std::cout << "It is a dog" << std::endl; }
};
 
struct Cat : Animal
{
   void what() const { std::cout << "It is a cat" << std::endl; }
};
 
struct Horse : Animal
{
   void what() const { std::cout << "It is a horse" << std::endl; }
};
 
 
int main()
{
    for ( Animal *p : { static_cast<Animal *>( new Dog() ),
                        static_cast<Animal *>( new Cat() ),
                        static_cast<Animal *>( new Horse() ) } )
    {
        p->what();
    }
    
    return 0;
}
so is IS possible !

but whats with the *> ?
i only know the ->
are they the same thing ?
There is no such operator as *> in C++. If you are speaking about construction
static_cast<Animal *>( new Horse() )

then Animal * is pointer type and <> denotes the type of casting of static_cast operator
oh sorry , now i get it
thank you very much !
Topic archived. No new replies allowed.