重新解释指针的指针转换

问题描述 投票:0回答:1

我有两个类,一个

Parent
类和一个
Child
类(继承自
Parent
类)我想知道是否可以使用
Child**
Parent**
 转换/转换为 
reinterpret_cast
 
还是其他什么? 或者这样的事情是不可能的

下面是一个最小的例子。我想知道代码是否正确或

Child**
Parent**
“转换”不正确且不应尝试?

NB:我的问题是关于指针的指针的转换。 IE。这条线正确吗?

Parent** ptrToChildPtr = reinterpret_cast<Parent**>(&childPtr);

我知道指针删除可以通过更简单的方式完成。我只想在这里展示我想要执行的转换(如果这种转换是可能的话)。

提前非常感谢

#include <iostream>
#include <memory>

class Parent {
public:
    Parent() {}
    virtual ~Parent() {
        std::cout << "Deleter of Parent class" << std::endl;
    }
};

class Child : public Parent {
public:
    Child() {}
    virtual ~Child() {
        std::cout << "Deleter of Child class" << std::endl;
    }
};

void deleterFunction(Parent** ptr) {
  if (*ptr){
    delete *ptr;
    *ptr = nullptr;
  }
}

int main() {
  Child* childPtr = new Child();
  Parent** ptrToChildPtr = reinterpret_cast<Parent**>(&childPtr);

  std::cout << "Before delete function: childPtr = " << childPtr << std::endl;
  deleterFunction(ptrToChildPtr);
  std::cout << "After delete function: childPtr = " << childPtr << std::endl;

  return 0;
}
c++ pointers reinterpret-cast
1个回答
0
投票

也许提供一个更具体的例子来说明为什么我想转换一些指针,因为我得到的答案并没有真正回答问题的核心

我有一个代码(不是我编码的,我不能改变它的结构,它太大了),其中基本上有很多不同类型的原始指针

Child1
Child2
,...
ChildN
继承自
Parent
类已初始化(因此使用
new
...因此应使用
delete
删除)

在代码中的不同时刻,我需要删除这些指针并再次初始化全部或几个指针。跟踪起来很痛苦,所以我想要一个

PointerHandler
类来负责初始化和删除分别调用
initPointer()
deleteAllPointers
函数的指针(如下所示),但为此我需要存储
PointerHandler
类中指针持有者的地址,因此存在关于转换地址的问题

#include <iostream>
#include <vector>
#include <string>
#include <memory>

class Parent {
public:
    Parent(const std::string & name): m_name(name) {}
    virtual ~Parent()  {
        std::cout << "Deleter of Parent class name=" << m_name << std::endl;
    }
    protected: 
      std::string m_name;
};

class Child1 : public Parent {
  public:
    Child1(const std::string & name): Parent(name) {}
    virtual ~Child1() {
        std::cout << "Deleter of Child1 class for name=" << m_name << std::endl;
    }
};

class Child2 : public Parent {
  public:
    Child2(const std::string & name): Parent(name) {}
    virtual ~Child2() {
        std::cout << "Deleter of Child2 class for name=" << m_name << std::endl;
    }
};


class PointerHandler{
  public: 
    PointerHandler(){}
    ~PointerHandler(){}

    // Template function to init the pointers
    template<class TChildClass> 
    void initPointer(const std::string &name, TChildClass * &childPtr){
      // Init the child pointer 
      childPtr = new TChildClass(name);

      // Keep track of the pointer adress so that it can be deleted 
      Parent** ptrToChildPtr = reinterpret_cast<Parent**>(&childPtr);
      m_vecPtrs.push_back(ptrToChildPtr);
    }

    void deleteAllPointers(){
      // Delete all pointers and reset pointer holder to nullptr 
      for (Parent **ptr: m_vecPtrs){
        deleterFunction(ptr);
      }
      m_vecPtrs.clear();
    }

  private: 
    std::vector<Parent**> m_vecPtrs;
    // Function used to delete pointers
    void deleterFunction(Parent** ptr) {
      if (*ptr){
        delete *ptr;
        *ptr = nullptr;
      }
    }
};

int main() {

  // Define pointer handler 
  PointerHandler ptrHdr;

  // Pointers to child classes 
  Child1* child1_0 = nullptr;
  Child1* child1_1 = nullptr;
  Child2* child2_0 = nullptr;
  Child2* child2_1 = nullptr;

  // Initialize pointers 
  ptrHdr.initPointer("child1_0", child1_0);
  ptrHdr.initPointer("child1_1", child1_1);
  ptrHdr.initPointer("child2_1", child2_0);
  ptrHdr.initPointer("child2_1", child2_1);

  std::cout << "Before deletion" << std::endl ;
  std::cout << "child1_0 ptr = " << child1_0 << std::endl;
  std::cout << "child1_1 ptr = " << child1_1 << std::endl;
  std::cout << "child2_1 ptr = " << child2_0 << std::endl;
  std::cout << "child2_1 ptr = " << child2_1 << std::endl;

  // Delete all pointers 
  ptrHdr.deleteAllPointers();

  std::cout << "After deletion" << std::endl ;
  std::cout << "child1_0 ptr = " << child1_0 << std::endl;
  std::cout << "child1_1 ptr = " << child1_1 << std::endl;
  std::cout << "child2_1 ptr = " << child2_0 << std::endl;
  std::cout << "child2_1 ptr = " << child2_1 << std::endl;

  // Init only few now poiters
  ptrHdr.initPointer("child1_0", child1_0);
  ptrHdr.initPointer("child1_1", child1_1);

  // deletion will occur when pointer handler is going out of scope

  return 0;
}

© www.soinside.com 2019 - 2024. All rights reserved.