casting error

This is a discussion on casting error within the C++ Programming forums, part of the General Programming Boards category; Hi, i got a base class,and 2 derieved classes.derieved1 and derieved 2. In the main class,i create a derieved1 and ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    58

    casting error

    Hi,
    i got a base class,and 2 derieved classes.derieved1 and derieved 2.

    In the main class,i create a derieved1 and derieve2 object. add it to vector vector<base*> list .

    Then i try casting out the first vector item to see if dereived 1 or derieved2. the tests to check if derieved 1 or 2 work for both.

    The classes:

    Code:
    class base{
        protected:
            base(int val){
                value=val;
            }
        public:
    	int value;
    };
    
    class derieved1:public base {
        private:
    	string data;
             
            
        public:
    	derieved1(int val,string x):base(val),data(x){ }
            void print(){
                 cout<<"\ndereieved 1";
            }
    };
    
    class derieved2:public base {
        private:
            string data;
        public:
            derieved2(int val,string x):base(val),data(x){
            }
            void print(){
                 cout<<"\ndereieved 2";
            }
    };
    the main function:
    Code:
    int main(int argc, char *argv[])
    {
        vector<base*> list;
        derieved1* a=new derieved1(1,"derieved1");
        derieved2* b=new derieved2(2,"derieved2");
        
        list.push_back(a);
        list.push_back(b);
        
        base* bbbb=list[0];
        
        //cast it to both derieved objects.    
        derieved1*sb=static_cast<derieved1*>(bbbb);
        
        derieved2*sb1=static_cast<derieved2*>(bbbb);
        
        //check which one is valid.
        
        if(sb){
               cout<<"object of type derieved1"<<endl;
        }
        if(sb1){
               cout<<"object of type derieved2"<<endl; //should not display
        }
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    The program prints outs :

    object of type derieved1
    object of type derieved2
    Last edited by rahulsk1947; 06-08-2009 at 06:36 PM.

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    1. Don't use system(). Very bad habit. Use getchar() or something...
    2. If I remember, sb1 points to SOMETHING so it returns true. Also not something you should do everyday. You may check and see if the information it points to is valid. I think....
    3. Don't forget to dereference the pointers using the delete operator.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You should be using dynamic_cast, not static_cast.

    dynamic_cast will return NULL for a bad pointer cast or throw an exception for a bad reference cast.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    58
    i convereted it to:

    derieved1*sb=dynamic_cast<derieved1*>(bbbb);
    derieved2*sb1=dynamic_cast<derieved2*>(bbbb);


    It gives me a error.

    'dynamic_cast' : 'base' is not a polymorphic type



    what should i modify in the base and derieved classes so that it becomes polymorphic?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    The base class should have at least one virtual function. The destructor should be virtual.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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