I'm trying to assign the address that a base class smart pointer is pointing to to a raw pointer of a derived class. The size of data that the base class pointer is pointing to is the same of the derived class.
Directory is the derived class and FileData is the base class.
Here is where I initialize the derived class in main
Directory temp;
Directory * currentDir = &temp;
I then try to do the following conversion after some non-relevant code.
if (typeid(files[i]) == typeid(Directory))
{
*this = static_cast<Directory *>(&files[i]);
}
where files is the following in the body of the Directory class
std::vector<std::shared_ptr<FileData>> files
and *this is
Directory * currentDir
I get the following error from the static_cast line
invalid static_cast from type ‘__gnu_cxx::__alloc_traits<std::allocator<std::shared_ptr<FileData> >, std::shared_ptr<FileData> >::value_type*’ {aka ‘std::shared_ptr<FileData>*’} to type ‘Directory*’
I even tried to dynamic_cast instead
if (typeid(files[i]) == typeid(Directory))
{
*this = dynamic_cast<Directory *>(&files[i]);
}
But it says that FileData (base class) is not of polymorphic type
cannot dynamic_cast, (source type is not polymorphic)
Despite the deconstructor of FileData being polymorphic here in the body of the FileData class via a virtual destructor
virtual ~FileData() = default;
Here is the deconstructor for the Directory (derived class)
~Directory() {};
Why won't either method work? Basically I'm trying to do the following
B* = static_cast<B*>(A*)
where B is the derived class and A is the base class.