Home > Software design >  How to create an interface to allow for the construction of different nested derived classes in C ?
How to create an interface to allow for the construction of different nested derived classes in C ?

Time:04-08

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);
        }
        
    };
};

CodePudding user response:

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

  • Related