Why dynamic_cast<> needs the class to has virtual methods?

This is a discussion on Why dynamic_cast<> needs the class to has virtual methods? within the C++ Programming forums, part of the General Programming Boards category; We have Code: class Derived : public Base{ }; int main(){ Base* b = new Base; Derived * d = ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Why dynamic_cast<> needs the class to has virtual methods?

    We have
    Code:
    class Derived : public Base{
    };
    
    int main(){
    
    	Base* b = new Base;
    
    	Derived * d = dynamic_cast< Derived *>(b);
    
    return 1;
    }
    Why the complie needs Base class has at least a virtual method?

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Derived * d = new Derived;
    Base* b = dynamic_cast<Base*>(d);
    //no problem
    //no virtual function needed in class Base

  3. #3
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    When a class has virtual functions, there are certain pieces of special runtime data created for objects of that class. When downcasting, dynamic_cast<> needs this data in order to do what it does.

    Code:
    Derived * d = new Derived;
    Base* b = dynamic_cast<Base*>(d);
    //no problem
    //no virtual function needed in class Base
    In this example, you're upcasting. This is equivilant to a static_cast<>, so no virtuals are needed.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Could you give me an example that using dynamic_cast to downcast and return value is not NULL?

  5. #5
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Quote Originally Posted by meili100 View Post
    Could you give me an example that using dynamic_cast to downcast and return value is not NULL?
    Code:
    class Fruit
    {
    public:
        virtual ~Fruit() { }
    };
    
    class Banana : public Fruit
    {
    };
    
    int main(int argc, char **argv)
    {
        Fruit *fp = new Banana;
        Banana *bp = dynamic_cast<Banana*>(fp);
    
        if (bp != NULL)
        {
            cout << "Banana detected!\n";
        }
    
        return 0;
    }
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,802
    Example
    Code:
    #include <iostream>
    
    
    class CBaseClass
    {
    public:
    	virtual void SayHello(){
    		std::cout<<"Hello From The Base"<<std::endl;
    	}
    };
    
    class CChildClass : public CBaseClass
    {
    public:
    	virtual void SayHello(){
    		std::cout<<"Hello From The Child"<<std::endl;
    	}
    };
    
    int main()
    {
    	CBaseClass *pBaseChild = new CChildClass;
    	CChildClass *pChild = NULL;
    
    	//This is a correct cast since pBaseChild is actally a child class
    	pChild = dynamic_cast<CChildClass*>(pBaseChild);
    
    	if(NULL != pChild){
    		pChild->SayHello();
    	}//if
    	else{
    		std::cout<<"Child Was NULL"<<std::endl;
    	}//else	
    
    	CBaseClass *pBaseBase = new CBaseClass;
    	CChildClass *pChildTwo = NULL;
    	
    	//This is an invalid cast because pBaseBase is actually a base class
    	pChildTwo = dynamic_cast<CChildClass*>(pBaseBase);
    	
    	if(NULL != pChildTwo){
    		pChildTwo->SayHello();
    	}//if
    	else{
    		std::cout<<"Child Two was NULL"<<std::endl;
    	}//else
    
    	//Clean up
    	delete pBaseChild;
    	delete pBaseBase;
    
    	std::cin.get();
    
    	return 0;
    }
    Woop?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Your example needs a virtual destrctor in the base class, prog-bman.

  8. #8
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Quote Originally Posted by Daved View Post
    Your example needs a virtual destrctor in the base class, prog-bman.
    And mine needs a delete statement. However, I used the phrase "Banana detected!" in a valid context, so it all cancels out.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual base class
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 03-14-2008, 07:45 AM
  2. Replies: 3
    Last Post: 10-31-2005, 11:05 AM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  4. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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