0

I need to have a variable struct inside a struct. So, I want to be able to include the data from any kind of struct in another struct. I think this is possible with a template but it isn't working out:

namespace BaseStructs
{
    template<typedef T>
    struct packet
    {
        int ID;
        T data;
    };
 }

So, this is what I have but if I make an object of this struct like this:

BaseStructs::packet packet;

it doesn't work because the program wants me to choose a template for THIS struct but I want the "data" variable to be changeable. Any ideas on how to solve this?

What I want to do is create a small object that holds an ID like in my example and I need to add extra data from another object to it (which might differ in number of variables and such).

Dries
  • 995
  • 2
  • 16
  • 45
  • 2
    This isn't clear; if you have a template, you have to instantiate it with parameters (e.g. `BaseStructs::packet packet`). What is the problem with that? – Oliver Charlesworth Apr 06 '14 at 21:58
  • But, if I do that. Would that be applied to the data variable INSIDE the packet struct? Or is it somehow applied to the BaseStruct::packet struct? – Dries Apr 06 '14 at 22:04
  • Well, isn't that how you use templates? – Dries Apr 06 '14 at 22:09
  • 1
    If this is supposed to work dynamically, you need to use something like Boost.Any or a hierarchy of classes with virtual functions. Templates require their instantiations to be fixed at compile time. – Philipp Apr 06 '14 at 22:11
  • 1) `typedef` should be `typename` or `class` 2) Should data have a static type (at compiletime!) or should it type be of dynamic nature (at runtime). I personally think that you want the first case, but its hard to tell without clarification from your side – Sebastian Hoffmann Apr 06 '14 at 22:15
  • a template wasn't really the way to go I noticed (see answer). I would've used a static type though. – Dries Apr 06 '14 at 22:25

2 Answers2

2

A template struct is just that - a template. It is impossible to initialize an instance of a template without giving it the specific type which it should be locked to. When this happens, a new concrete type is created from the templte.

For example, the code:

BaseStructs::packet<Foo> fooPacket;
BaseStructs::packet<Bar> barPacket;

would cause the compiler to create two new concrete types with the following definitions:

struct
{
    int ID;
    Foo data;
};
struct
{
    int ID;
    Bar data;
};

The original template struct still exists. Neither of these new types cause it to change its meaning.

Tyler Gill
  • 861
  • 6
  • 9
1

If you are more to a C style, you can use

 void* data;

than, of course, you will need to cast to a specific type everywhere you use BaseStructs::packet

Another way is to serialize your struct to string.

Or just store a std::map instead of your structs.

If you are more object oriented, you should try to use polymorphism. Try to find smt. common in all your structs. It might be behaviour. Than apply an apropriate design pattern http://en.wikipedia.org/wiki/Design_pattern_(computer_science)

kepkin
  • 1,105
  • 11
  • 14
  • It wasn't really the answer but you helped me anyway. I now am able to add structs to this struct that are a child of a specific other struct. (If that's the right way to say it) so instead of T I now have BaseStruct and I can add structs that inherit from that type of struct – Dries Apr 06 '14 at 22:13
  • Just in case: beware of slicing http://stackoverflow.com/questions/274626/what-is-the-slicing-problem-in-c – kepkin Apr 06 '14 at 22:20
  • Hmm interesting! Thanks for that! – Dries Apr 06 '14 at 22:24