0

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.

  • So you want to assign `std::shared_ptr` to `*this`? – Fureeish Sep 07 '19 at 23:49
  • Yes, I'm trying to assign FileData * to Directory * (*this). – CuriousWalrus Sep 07 '19 at 23:51
  • @CuriousWalrus That should not be a "yes" answer. `FileData*` and `std::shared_ptr` are different types and `*this` is of type `Directory`, not `Directory*`. Assigning a pointer to it won't work in any case. – walnut Sep 07 '19 at 23:53
  • Sorry for being confusing, I basically want to do `B* = static_cast(A*)` where B is the derived class and A is the base class. Doing `*this = files[i].get()` gives me the following error `no known conversion for argument 1 from ‘std::__shared_ptr::element_type*’ {aka ‘FileData*’} to ‘const Directory&’` – CuriousWalrus Sep 07 '19 at 23:55
  • 1
    @CuriousWalrus You cannot assign a pointer to `*this`, which is always a non-pointer type. Do you want to assign the referenced object instead? – walnut Sep 07 '19 at 23:56
  • @uneven_mark Ah ok, yes thats what I want to do. I basically want the address of files[i] to be pointed by this, where the calling object is currentDir (Directory *) if thats possible – CuriousWalrus Sep 08 '19 at 00:00
  • @CuriousWalrus Your second sentence contradicts your answer to my question. You cannot modify the location `this` points to. All of this seems like [a X-Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). It would probably be more fruitful if you described your initial problem/goal and your current (non-working) approach to it. – walnut Sep 08 '19 at 00:09
  • @uneven_mark `*this = *static_cast(files[i].get());` COMPILES!! But you are right, `typeid(files[i]) == typeid(Directory)` does not work the way I want it to, and it just skips it everytime. I'm trying to make it where the currentDir points to a Directory class instead of a FileData class. – CuriousWalrus Sep 08 '19 at 00:09
  • @CuriousWalrus I removed that comment, because it does *not* change where `this` points. That line calls the assignment operator to assign the right-hand instance to the left-hand one, they will be different instances and `this` will not equal `files[i].get()`. See my modified comment above. – walnut Sep 08 '19 at 00:11
  • Alright, I understand what your saying. I'm trying to implement the linux shell commands where directories have a pointer to their parent directory and a vector of FileData which includes information about a directory or file (such as name, owner, etc) Directory inherits from that class. My problem here lies where I'm trying to implement the cd command. My solution I tried here was having a current directory pointer in main and then changing it where it points to in the cd function by using it as the calling object. – CuriousWalrus Sep 08 '19 at 00:24
  • If you want to change what `currentDir` points to, then you need to assign to `currentDir`. The claim *"`*this` is `currentDir`"* can't possibly be true: `currentDir` is a pointer of type `Directory*`, while `*this` is of type `Directory` - not a pointer. – Igor Tandetnik Sep 08 '19 at 17:54

0 Answers0