简体   繁体   English

将使用基类ctor创建的对象转换为派生类

[英]convert an object created with base class ctor to a derived class

I have the following classes: 我有以下课程:

class Base {
public:
  virtual ~Base(){}
  Base() {}

  virtual void foo() = 0;
};

class Derived : public Base {
public:
  virtual ~Derived(){}
  Derived() : Base() {}

  void foo() { printf("derived : foo\n"); }
};

class IInterface {
public:
  virtual ~IInterface() {}

  virtual void bar() = 0;
};

class C : public Derived, public IInterface {
public:
  virtual ~C(){}
  C() : Derived(){}

  void bar() { printf("C : bar\n"); }
};

now I have a bunch of Derived* objects and I want to apply different interfaces on them : 现在我有一堆Derived*对象,并且我想在它们上应用不同的接口:

  Derived* d = new Derived();
  C* c = dynamic_cast<C*>(d);
  c->bar();
  c->foo();

dynamic_cast returns nullptr and with c-style cast i get seg fault. dynamic_cast返回nullptr并使用c样式强制转换会出现段错误。 is there anyway to achieve this? 反正有实现这一目标的方法吗?

note that my objects are already created with Derived ctor. 请注意,我的对象已经使用Derived ctor创建了。 i just want to treat them differently using Interfaces 我只想使用接口来区别对待它们

实现此目的的唯一方法是创建一个新对象,然后将数据从旧对象移出。

Try encapsulating the behaviour that needs to change at runtime. 尝试封装在运行时需要更改的行为。 Instead of inheriting from the IInterface, you have a member variable that is an IInterface pointer. 而不是从IInterface继承,而是具有一个成员变量,该成员变量是IInterface指针。 Then instead of overriding bar in the child class, you pass the call to bar through to whatever is being pointed at. 然后,您可以将对bar的调用传递给所指向的对象,而不是覆盖子类中的bar。 This allows modular behavior that looks just like polymorphism, but is more flexible: 这允许看起来像多态的模块化行为,但是更加灵活:

class IInterface {
public:
  virtual ~IInterface() {}

  virtual void bar() = 0;
};
class Derived : public Base {
public:
  IInterface* m_bar;

  virtual ~Derived(){}
  Derived() : Base(){}

  void bar() {return m_bar->bar(); }
  void foo() { printf("derived : foo\n"); }
};

You then derive and create IInterface objects and can associate any of them with Derived objects. 然后,您可以派生并创建IInterface对象,并将它们中的任何一个与派生对象相关联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实例化派生类对象,其基类ctor是私有的 - Instantiate a derived class object, whose base class ctor is private 在派生类中使用基类Copy CTOR - Use base class Copy CTOR in derived class 在派生类中使用虚拟基类中的受保护ctor - use protected ctor from virtual base class in derived class 派生类中基类委派的ctor的c ++使用 - c++ usage of delegated ctor from base class in derived class 将基类指针分配给免费商店中创建的派生类对象 - assigning a base class pointer to a derived class object created in the free store 使用基本 class 指针创建 object 时缺少派生 class 析构函数 - Missing Derived class Destructor when object is created with the base class pointer 虚拟继承:Base Ctor没有调用Most Derived Class? - Virtual Inheritance : Base Ctor not calling in Most Derived Class? “最派生类的ctor需要直接调用虚拟基类的ctor”语句的原始内容在哪里? - Where is the original of the statement “the most derived class's ctor needs to directly call the virtual base class's ctor”? 如何使用派生Ctor中模板基类的typedef? - How to use typedef from template base class in Ctor of the Derived? 将对象转换为基类还是派生类? - Cast object to base or derived class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM