c++设计查询

发布于 2024-12-24 02:26:01 字数 1269 浏览 0 评论 0原文

我正在用 C++ 设计一个框架,它应该提供基本功能并充当其他派生系统的接口。

#include <stdio.h>

class Module
{
  public:
    virtual void print()
    {
        printf("Inside print of Module\n");
    }
};
class ModuleAlpha : public Module
{
  public:
    void print()
    {
        printf("Inside print of ModuleAlpha\n");
    }
    void module_alpha_function()  /* local function of this class */
    {
        printf("Inside module_alpha_function\n");
    }
};
class System
{
  public:
    virtual void create_module(){}
   protected:
    class Module * module_obj;
};
class  SystemAlpha: public System
{
  public:
    void create_module()
    {
        module_obj = new ModuleAlpha();
        module_obj->print(); // virtual function, so its fine.

        /* to call module_alpha_function, dynamic_cast is required,
         * Is this a good practice or there is some better way to design such a system */
        ModuleAlpha * module_alpha_obj = dynamic_cast<ModuleAlpha*>(module_obj);
        module_alpha_obj->module_alpha_function();
    }
};

main()
{
   System * system_obj = new SystemAlpha();
   system_obj->create_module();
}

编辑代码使其更加合乎逻辑,并且可以立即编译。问题是,是否有更好的方法来设计这样的系统,或者dynamic_cast是唯一的解决方案。此外,如果有更多派生模块,则对于类型转换,基 Module 类中需要一些智能。

I am designing a framework in c++ which is supposed to provide basic functionality and act as interface for the other derived systems.

#include <stdio.h>

class Module
{
  public:
    virtual void print()
    {
        printf("Inside print of Module\n");
    }
};
class ModuleAlpha : public Module
{
  public:
    void print()
    {
        printf("Inside print of ModuleAlpha\n");
    }
    void module_alpha_function()  /* local function of this class */
    {
        printf("Inside module_alpha_function\n");
    }
};
class System
{
  public:
    virtual void create_module(){}
   protected:
    class Module * module_obj;
};
class  SystemAlpha: public System
{
  public:
    void create_module()
    {
        module_obj = new ModuleAlpha();
        module_obj->print(); // virtual function, so its fine.

        /* to call module_alpha_function, dynamic_cast is required,
         * Is this a good practice or there is some better way to design such a system */
        ModuleAlpha * module_alpha_obj = dynamic_cast<ModuleAlpha*>(module_obj);
        module_alpha_obj->module_alpha_function();
    }
};

main()
{
   System * system_obj = new SystemAlpha();
   system_obj->create_module();
}

Edited the code to be more logical and it compiles straight away. The question is, that is there a better way to design such a system, or dynamic_cast is the only solution. Also, if there are more derived modules, then for type-casting, there is some intelligence required in the base Module class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清晰传感 2024-12-31 02:26:01

如果 DerivedBase 的唯一具体实例,您可以使用 static_cast 代替。

就我个人而言,我为每个专门的类定义了一个函数,例如 MyCast。我定义了四个重载变体,以便我可以向下转换 const 和非 const 指针和引用。例如:

  inline Derived       * MyCast(Base       * x) { return static_cast<Derived *>      (x); }
  inline Derived const * MyCast(Base const * x) { return static_cast<Derived const *>(x); }
  inline Derived       & MyCast(Base       & x) { return static_cast<Derived &>      (x); }
  inline Derived const & MyCast(Base const & x) { return static_cast<Derived const &>(x); }

Derived2 和 Base2 也是如此。

拥有这四个的最大优点是,您不会意外更改常量,并且无论您有指针还是引用,都可以使用相同的构造。

当然,您可以用宏替换static_cast,并在调试模式下使用dynamic_cast,而在发布模式下使用static_cast

此外,上面的代码可以轻松地包装到宏中,从而可以轻松地批量定义函数。

使用此模式,您可以将代码实现为:

class Derived : public Base
{
public:
   virtual void func2() 
   { 
     base2_obj = new Derived2();
   }
   void DerivedFunc()
   {
     MyCast(base2_obj)->Derived2Func();
   }
}

If Derived is the only concrete instance of Base you could use static_cast instead.

Personally, I define a function, like MyCast for every specialized class. I define four overloaded variants, so that I can down-cast const and non-const pointers and references. For example:

  inline Derived       * MyCast(Base       * x) { return static_cast<Derived *>      (x); }
  inline Derived const * MyCast(Base const * x) { return static_cast<Derived const *>(x); }
  inline Derived       & MyCast(Base       & x) { return static_cast<Derived &>      (x); }
  inline Derived const & MyCast(Base const & x) { return static_cast<Derived const &>(x); }

And likewise for Derived2 and Base2.

The big advantage in having all four is that you will not change constness by accident, and you can use the same construct regardless if you have a pointer or a reference.

Of course, you could replace static_cast with a macro, and use dynamic_cast in debug mode and static_cast is release mode.

Also, the code above can easily be wrapped into a macro, making it easy to batch-define the functions.

Using this pattern, you could then implement your code as:

class Derived : public Base
{
public:
   virtual void func2() 
   { 
     base2_obj = new Derived2();
   }
   void DerivedFunc()
   {
     MyCast(base2_obj)->Derived2Func();
   }
}
勿忘初心 2024-12-31 02:26:01

如果 Base 不包含 base_obj 对象,而是通过虚拟方法获取引用,那么设计会变得更加清晰。 Derived 应包含一个 Derived2 对象,例如:

class Base
{
  public:
      virtual void func1();
  private:
      class Base2;
      virtual Base2& get_base2();
};
class Derived : public Base
{
         Derived2 derived2;
   public:
         Base2& get_base2() { return derived2; }
         void DerivedFunc()
         {
              derived2->Derived2Func();
         }
}

如果您担心性能,请在 Base 的构造函数中传递引用。

The design gets much cleaner if Base does not contain the base_obj object, but rather gets a reference via a virtual method. Derived should contain a Derived2 object, like:

class Base
{
  public:
      virtual void func1();
  private:
      class Base2;
      virtual Base2& get_base2();
};
class Derived : public Base
{
         Derived2 derived2;
   public:
         Base2& get_base2() { return derived2; }
         void DerivedFunc()
         {
              derived2->Derived2Func();
         }
}

If you are worried about performance, pass the reference in the constructor of Base.

冷月断魂刀 2024-12-31 02:26:01

我采用了您的代码及其许多编译错误并尝试简化它。这就是你想要达到的目标吗?它将编译。

class Base2 {
  public:
     virtual void Derived2Func(){
     }
};

Base2* fnToInstantiateABase2();

class Base {
  public:
     Base()  : base2_obj(fnToInstantiateABase2()) {
     }
     virtual void DerivedFunc() {
     }
  protected:
     Base2* base2_obj;
};
class Derived : public Base {
  public:
     void DerivedFunc() {
        base2_obj->Derived2Func(); // not possible as base2_obj is of type Base2
     }
};


class Derived2 : public Base2 {
  public:
     void Derived2Func() {
     }
};


void test() {
  Base * base_obj = new Derived();
  base_obj->DerivedFunc();
}

Base2* fnToInstantiateABase2() {
  return new Derived2();
}

I took your code with its many compile errors and tried to simplify it. Is this what you are trying to acheive? It will compile.

class Base2 {
  public:
     virtual void Derived2Func(){
     }
};

Base2* fnToInstantiateABase2();

class Base {
  public:
     Base()  : base2_obj(fnToInstantiateABase2()) {
     }
     virtual void DerivedFunc() {
     }
  protected:
     Base2* base2_obj;
};
class Derived : public Base {
  public:
     void DerivedFunc() {
        base2_obj->Derived2Func(); // not possible as base2_obj is of type Base2
     }
};


class Derived2 : public Base2 {
  public:
     void Derived2Func() {
     }
};


void test() {
  Base * base_obj = new Derived();
  base_obj->DerivedFunc();
}

Base2* fnToInstantiateABase2() {
  return new Derived2();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文