Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8647565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:06:11+00:00 2026-06-12T13:06:11+00:00

I have several datacontainertype like this but they differ in their content: struct containerRequest

  • 0

I have several datacontainertype like this but they differ in their content:

struct containerRequest
{
   uint8_t data1
   uint8_t data2
};

struct containerResponse
{
    uint8_t data1;
    uint8_t data2;
};

union myType
{
   containerRequest Request;
   containerResponse Response;
};

Now I would like to create an abstract interface to ensure all of this interfaces have an MyDatatype like this

class IMyInterface
{
   public: 
      union myType
      {
          X Request;
          Y Response;
      };


      bool IsMyType(myType& data) = 0;
      void DoThings(myType& data) = 0;
};

I thought of using template but this brakes the rest of my code:

template <typename X, typename Y>
class IMyInterface
{
    ...
}

but this breaks the rest of my code.

Because I have a collection of the different containers:

void addContainer(IMyInterface& data)
{
     collection.push_back(&data);
}

when I use Templates in the interface g++ forces me to specify the types of my templates.

How can I define X and Y in my derived class without the need to specific the type in my collection?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:06:12+00:00Added an answer on June 12, 2026 at 1:06 pm

    Sounds like you need (runtime) polymorphism, i.e. want code which deals with an IMyInterface without knowing it’s precise type. Templates alone won’t get you there, since they only provide compile-time polymorphism. The parts of C++ which provide what you want are RTTI (run-time type information) and virtual functions. One of the extremely powerfull features of C++ is that you can combine the two approaches. In your case, that means turning IMyInterface into a template, but letting that template inherit from a non-templated base class which defines the virtual functions you need. Here’s an example.

    // Base class for interface types. Make sure it's a polymorphic type by adding a least one
    // virtual member. Since polymorphich types should have virtual destructory anyway, adding
    // a virtual no-op destructor is the natural choice.
    struct IMyInterfaceDataType {
      virtual ~IMyInterfaceDataType() {};
    };
    
    // Base class for interface implementations.
    struct IMyInterface {
      virtual bool IsMyType(const IMyInterfaceDataType& data);
      virtual void DoThings(IMyInterfaceDataType& data);
    }
    
    template<typename RequestType, typename ResponseType>
    struct IMyInterfaceImplementation : MyInterface {
      struct MyType : IMyInterfaceDataType {
        union { RequestType Request; ResponseType Response } data;
    
        // You'll probably want to add some constructors here
      };
    
      virtual bool IsMyType(const IMyInterfaceDataType& data) {
        // Cast as pointer, not reference, to get NULL instead of an exception if cast fails
        return dynamic_cast<const MyType*>(&data) != NULL;
      }
    };
    
    struct SomeRequestType { ... };
    struct SomeResponseType { ... };
    struct SomeInterface : IMyInterfaceImplementation<SomeRequestType, SomeResponseType> {
      virtual void DoThings(IMyInterfaceDataType& data_raw) {
        // Throws std::bad_cast if data_raw isn't actually a DataType instance
        DataType& data = dynamic_cast<DataType&>(data_row);
        ... Do whatever you want to do with data ...
      } 
    };
    
    // This should now work, but you need to store pointers (or references, but
    // putting references into a std::vector doesn't work), since polymorphism only
    // works as expected with references or pointers.
    std::vector<IMyInterface*> interfaces;
    void addCollection(IMyInterface* iface) {
      interfaces.push_back(iface);
    }
    
    // This would then process an arbitrary data type with the first matching interface
    void process(IMyInterfaceDataType& data) {
      for(std::vector<IMyInterface>::iterator i = interfaces.begin(); i != interfaces.end(); ++i) {
        IMyInterface& iface = **i;
        if (iface.IsMyType(data) {
          iface.DoThings(data);
          break;
        }
      }
    }
    

    In real code, you should of course not store raw points in the interfaces vector, but instead use some kind of smart pointer. boost::shared_ptr (or std::shared_ptr if you’re using C++11) would be a good choice. You might also want to take a look at boost::any, which wraps objects of arbitrary type and a safe manner (i.e., in a way, it’s void* done right).

    Your use of union is a bit dubious, btw. How would a DoThings method know whether the data contains a request or a response? You might want to consider using boost::variant instead of a C-style union.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several entities in datastore, but I don't know if some of their
I have several xml files that are formated this way: <ROOT> <OBJECT> <identity> <id>123</id>
I have several UITableViews and what I would like to do is to be
I have several listviews with two collumns like. I am building a stats application
Have several questions. Don't flame me - I'm newbie, but eager to know more.
I have Several UIImageViews in a NSMutableArray. They are all in the superView. I
I have several compiled python modules; they are put into a single .so (to
I have several problems relating to NSPredicates, here are they; 1.) My code is
I have several div's that have their html modified during the life of the
I have several buttons. When clicked I would like to remove all classes from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.