Using rtti (help!!!!!!!)

This is a discussion on Using rtti (help!!!!!!!) within the C++ Programming forums, part of the General Programming Boards category; Code: // Listing 14.2 Using dynamic_cast. // Using rtti #include <iostream> using namespace std; enum TYPE { HORSE, PEGASUS };//doesn't ...

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Using rtti (help!!!!!!!)

    Code:
    // Listing 14.2 Using dynamic_cast.
    // Using rtti
    
    #include <iostream>
    using namespace std;
    
    enum TYPE { HORSE, PEGASUS };//doesn't seem to be really needed for this
    //project to run, but oh well, That's Jesse Liberty for you right there.
    
    class Horse
    {
    public:
    	virtual void Gallop(){ cout << "Galloping...\n"; }
    
    private:
    	int itsAge;
    };
    
    class Pegasus : public Horse
    {
    public:
    
    	virtual void Fly() {cout<<"I can fly! I can fly! I can fly!\n";}
    };
    
    const int NumberHorses = 5;
    int main()
    {
    	Horse* Ranch[NumberHorses];
    	Horse* pHorse;
    	int choice,i;
    	for (i=0; i<NumberHorses; i++)
    	{
    		cout << "(1)Horse (2)Pegasus: ";
    		cin >> choice;
    		if (choice == 2)
    			pHorse = new Pegasus;
    		else
    			pHorse = new Horse;
    		Ranch[i] = pHorse;
    	}
    	cout << "\n";
    	for (i=0; i<NumberHorses; i++)
    	{
    		Pegasus *pPeg = dynamic_cast< Pegasus *> (Ranch[i]);
             /* What is this saying here?*/
    
    		if (pPeg)// what is this saying here?
                    /* is it asking to say "if pReg= dynamic_cast< Pegasus *> (Ranch[i])
                    then call the fly method or what?*/
    			pPeg->Fly();
    		else
    			cout << "Just a horse\n";
    
    		delete Ranch[i];
    	}
     int x; cin>>x;
    	return 0;
    }
    
    /* Thank you all in advance for you help *
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >/* is it asking to say "if pReg= dynamic_cast< Pegasus *> (Ranch[i]) then call the fly method or what?*/

    Yes, if Ranch[i] is a Pegasus the cast will be valid otherwise it will fail and pReg will point to NULL (if Ranch[i] is only a Horse).

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    oh thanks that was easy, glad to know I was on the right track.

    take this for example

    Pegasus *pPeg = dynamic_cast< Pegasus *> (Ranch[i]);

    could you tell me the syntax for this..........because like this dynamic_cast< Pegasus *> I don't really know what this is exactly doing if I had to use some other time.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >could you tell me the syntax for this

    It's the C++ casting syntax.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RTTI for distribution
    By coletek in forum C++ Programming
    Replies: 6
    Last Post: 09-13-2010, 11:05 AM
  2. Eliminating dynamic_cast<>
    By skewray in forum C++ Programming
    Replies: 22
    Last Post: 09-14-2007, 05:58 PM
  3. Problem with RTTI and a factory method
    By philvaira in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2007, 04:19 PM
  4. RTTI - typeid()
    By vb.bajpai in forum C++ Programming
    Replies: 2
    Last Post: 06-23-2007, 10:12 PM
  5. RTTI - Printing template arg that could be array
    By LuckY in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2005, 04:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21