C ++: Casting from a parent class to a daughter?

1

I have two classes lib_array_list that inherit from lib_object :

class lib_array_list : public lib_object
{
    size_t len;
    size_t sizememory;
    lib_object** objs;//es un array que cada tipo es de lib_object
    std::string name;

    public:


    lib_array_list():len{0},sizememory{4}, objs{new lib_object*[4]}
    {
         name = "noname";
    }

    lib_array_list(std::string name)
    :name{name},len{0},sizememory{4}, objs{new lib_object*[4]}
    {
    }

    std::string get_array_list_name(){
         return name;
    }

    lib_object& get_array_list_by_name(std::string name){
         return get(0);
    }

    lib_object& get(size_t pos){

        return *(objs[pos]);
    }

My lib_object class:

class lib_object{


public:

lib_object()
{

}
virtual ~lib_object()
{
}
virtual std::string to_string() const
{
     return "x";
}
virtual bool operator ==(const lib_object& obj) const
{
     return false;
}
virtual std::string get_class_name()const
{
     return "lib_object";
}

};

My class array_list can store objects of the same type, that is arrayists arraylists and in my class lib_array_list I have a get_array_list_by_name function that at the moment returns a lib_object& of the first position and what I need is to do a casting to lib_array_list to be able to operate on it, but I have an error in execution: Aborted (core dumped) My casting is doing so:

lib_array_list r = (lib_array_list&)r.get_array_list_by_name("students");

How should I cast from lib_object to lib_array_list ?

    
asked by maudev 31.07.2018 в 18:37
source

2 answers

0

Cast to child classes must be done with dynamic_cast the explanation is in item 5 of the explanation of dynamic_cast reference (" link "), it is also possible to do downcast using static_cast but it does not check types at run time. (cast insecure).

An example of the use would be: Let A be a base class and B a derivative of A

 A * pB = new B();
 B * pB2 = dynamic_cast<B*>(pB); 

In your case a simple one will be enough.

lib_array_list & ref = dynamic_cast<lib_array_list&>(r.get_array_list_by_name("students"));

Greetings.

    
answered by 01.08.2018 / 00:36
source
1

The error you receive usually has to do with memory management not with conversion problems in the inheritance tree.

Without seeing how you fill in data of your lib_array_list and how you use it and empty it is difficult to make conjectures, the usual thing for that type of error is usually a double release of memory.

On the other hand, you're doing things wrong, you're copying a base class in a derivative:

//             v <--- Instancia
lib_array_list r = (lib_array_list&)r.get_array_list_by_name("students");
//                  ^^^^^^^^^^^^^^^ <--- Referencia

As far as I could see, the class lib_array_list does not have copy constructors for class lib_object so a priori, you will not know how to make that copy. Surely what you wanted was a reference:

lib_array_list &r = (lib_array_list&)r.get_array_list_by_name("students");

It is possible that when making the copies, you are invoking the destructors of some temporary objects and this causes you the memory error.

To finish, avoid the data conversions of type C ( (tipo)dato ) and use the C ++ ( xxxxxx_cast<tipo>(dato) ); those of C ++ are more secure.

    
answered by 01.08.2018 в 08:44