如何创建一个接口以允许在 C 中构造不同的嵌套派生类?

发布于 2025-01-19 14:07:19 字数 2787 浏览 1 评论 0原文

我的目标是从接口构建一个派生的类嵌套类。但是,嵌套类没有相同的构造函数。问题是如何制作一个接口来创建两个不同的“子巢”类。

约束:

  • 之前,无法使用堆积
  • 在构造
  • C ++ 17
ITest::INestedTest* MakeTest(ITest* test, ITest::Config config)
{
  // Can't call directly because it's not on the interface i.e. test.InitializeNestedTest ...
  // Only workable situation is this:
  if (condition)
  {
     auto myTest = static_cast<Test2::Test*>(test);
     int p = 2;
     return myTest->InitalizeNestedTest(config, p);
     // ERROR function returning abstract class not allowed
  } else {
     auto myTest = static_cast<Test1::Test*>(test);
     return myTest->InitalizeNestedTest(config); 
     // ERROR function returning abstract class not allowed
  }   
  
}

类的方法无法调用。这种静态铸件没有返回我以前想要的东西,因为我将指针返回到本地定义的变量,这在评论中指出。由于它是一个抽象的类,我如何才能从中返回一堂课,我需要再次施放它还是做出多个功能?

Test1::Test myTest; 
auto myNestedTest = myTest.InitializeNestedTest(config);

我已经想到了一些选项,但它们看起来都不正确,或者我不确定如何实现它们

  1. 具有界面上每种类型的虚拟功能,然后在子类上覆盖它们(如果可能的话,似乎不是正确的方法)
  2. 扩展了Config Struct test2名称空间,以便它包含参数P,因此它们都具有相同的原型并将其放在接口上。 (是否可以从接口中“扩展“ struct”?)
  3. 也许使用不同类型的铸件,或以不同的方式进行?

我包括了我的接口的定义和两个子类以供参考。

class ITest
{
    //other things in ITest.hpp not relevant to question
public:
  struct Config
  {
      int a;
      bool enable;
  };
    class INestedTest
    {
    public:
        virtual void Enable() const = 0;
        virtual void Configure(Config const& config)
        {
            if(config.enable)
            {
                Enable();
            }
        }
    };
};

namespace Test1
{
    class Test : public ITest
    {
    public:
        class NestedTest : public ITest::INestedTest
        {
        public:
            NestedTest(Config const& config)
            {
                Configure(config);
            }
            void Enable() const override
            {
                //impl
            }
        }; // End NestedTest
        
        NestedTest InitalizeNestedTest(Config const& config)
        {
            return NestedTest(config);
        }
        
    };
};

namespace Test2
{
    class Test : public ITest
    {
    public:
        class NestedTest : public ITest::INestedTest
        {
        public:
            using Parameter = int;
            NestedTest(ITest::Config const& config, Parameter p)
            {
                Configure(config);
            }
            void Enable() const override
            {
                //impl
            }
        }; // End NestedTest
        
        NestedTest InitalizeNestedTest(Config const& config, NestedTest::Parameter p)
        {
            return NestedTest(config, p);
        }
        
    };
};

My goal is to construct a derived classes nested class from the interface. However the nested classes don't have the same constructors. The question is how can I make an interface to create two different "sub-nested" classes.

Constraints:

  • Cannot use Heap
  • Nested Classes' Methods cannot be called before it is constructed
  • C++ 17
ITest::INestedTest* MakeTest(ITest* test, ITest::Config config)
{
  // Can't call directly because it's not on the interface i.e. test.InitializeNestedTest ...
  // Only workable situation is this:
  if (condition)
  {
     auto myTest = static_cast<Test2::Test*>(test);
     int p = 2;
     return myTest->InitalizeNestedTest(config, p);
     // ERROR function returning abstract class not allowed
  } else {
     auto myTest = static_cast<Test1::Test*>(test);
     return myTest->InitalizeNestedTest(config); 
     // ERROR function returning abstract class not allowed
  }   
  
}

This static cast didn't return what I wanted previously because I was returning a pointer to a locally defined variable, which was pointed out in the comments. How am I able to return a class from this since it's an abstract class, do i need to cast it again or make multiple functions?

Test1::Test myTest; 
auto myNestedTest = myTest.InitializeNestedTest(config);

I've thought of a few options but none of them seem right, or I'm not entirely sure how to implement them

  1. Have an overloaded Virtual function for each type on the interface and then override them on the subclass (not sure if possible and doesn't seem like the right way to do it)
  2. Extend the Config struct Test2 namespace so that it includes parameter p, so that they all have the same prototype and put it on the interface. (is it possible to "extend" the struct" from the interface?)
  3. Maybe use a different type of cast, or do so in a different way?

I've included the definitions of my Interface and two subclasses for reference.

class ITest
{
    //other things in ITest.hpp not relevant to question
public:
  struct Config
  {
      int a;
      bool enable;
  };
    class INestedTest
    {
    public:
        virtual void Enable() const = 0;
        virtual void Configure(Config const& config)
        {
            if(config.enable)
            {
                Enable();
            }
        }
    };
};

namespace Test1
{
    class Test : public ITest
    {
    public:
        class NestedTest : public ITest::INestedTest
        {
        public:
            NestedTest(Config const& config)
            {
                Configure(config);
            }
            void Enable() const override
            {
                //impl
            }
        }; // End NestedTest
        
        NestedTest InitalizeNestedTest(Config const& config)
        {
            return NestedTest(config);
        }
        
    };
};

namespace Test2
{
    class Test : public ITest
    {
    public:
        class NestedTest : public ITest::INestedTest
        {
        public:
            using Parameter = int;
            NestedTest(ITest::Config const& config, Parameter p)
            {
                Configure(config);
            }
            void Enable() const override
            {
                //impl
            }
        }; // End NestedTest
        
        NestedTest InitalizeNestedTest(Config const& config, NestedTest::Parameter p)
        {
            return NestedTest(config, p);
        }
        
    };
};

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

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

发布评论

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

评论(1

坦然微笑 2025-01-26 14:07:19

也许您可以使对象静态,因此在编译时(而不是堆或堆栈)在RAM中声明。

Maybe you could make the object static so it's declared in RAM at compile time (and not heap or stack).

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